Hi I'm trying to teach myself python and so far to good, but I'm having
a bit of trouble getting a function to work the way I think it should
work. Right now I'm taking a simple program I wrote in Fortran and
trying to do it in Python. I got it to work, but now I'm trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I'm still new so I'm having
trouble understanding what I'm doing wrong. Here is my code:
#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.
import Numeric
def abc(array1,arra y2,dotprod,sum, maxvalue):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array."""
#TODO: Get this damn thing working!!
print "Array 1 in:",array1 #Debug
data = Numeric.zeros(( 10,10))
for i in range(10):
for j in range(10):
data[i,j] = (i+1)+(j+1)
e = Numeric.matrixm ultiply(data, array1)
g = Numeric.matrixm ultiply(Numeric .transpose(data ),array2)
array1 = e
array2 = g
dotprod = Numeric.dot(arr ay1,array2)
sum = Numeric.sum(arr ay1)
maxvalue = array2[Numeric.argmax( array2)]
print "Array 1 out:",array1 #Debug
return array1,array2,d otprod,sum,maxv alue #<<-- Not working as I
thought it would.
x = Numeric.arange( 1,11)
y = Numeric.arange( 1,11)*2
dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?
print 'Array X:',x
print 'Array Y:',y
abc(x,y,dotp,su mx,maxv)
print 'Calling function abc'
print 'Array X:',x
print 'Array Y:',y
print 'Dot Product of X and Y:',dotp
print 'Sum of array X:',sumx
print 'Max value of array Y:',maxv
If you run it the data gets passed to the function just fine and it
finds the right numbers. Its just getting it to pass them back thats
not working. I put some print statements inside the function just to
see how the data gets passed. So any ideas that would help me? If you
want to see the fortran code just email me.
David 32 2518
On 2005-05-14, David wrote: abc(x,y,dotp,su mx,maxv)
(x,y,dotp,sumx, maxv) = abc(x,y,dotp,su mx,maxv)
Bernd
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]
Hi,
You're thinking you're passing the arguments as reference (look at mutable
vs non-mutable)
Your function returns the values in a tupple (x,y,...); you need to fetch
the values from that tupple
Regards,
Philippe
David wrote: Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in Fortran and trying to do it in Python. I got it to work, but now I'm trying to modularize it. My fortran program uses a subroutine and I was trying to do the same thing in Python. But I'm still new so I'm having trouble understanding what I'm doing wrong. Here is my code:
#! /usr/bin/python #This program takes two vectors and multiplies them #against a 10X10 array. It also then gives the dot product, #sum, and max value of the array.
import Numeric def abc(array1,arra y2,dotprod,sum, maxvalue): """Takes two arrays and performs predetermined calculations, Then returns the solutions back as the same array, along with the dot product, sum, and max value of the array.""" #TODO: Get this damn thing working!! print "Array 1 in:",array1 #Debug data = Numeric.zeros(( 10,10)) for i in range(10): for j in range(10): data[i,j] = (i+1)+(j+1)
e = Numeric.matrixm ultiply(data, array1) g = Numeric.matrixm ultiply(Numeric .transpose(data ),array2) array1 = e array2 = g dotprod = Numeric.dot(arr ay1,array2) sum = Numeric.sum(arr ay1) maxvalue = array2[Numeric.argmax( array2)] print "Array 1 out:",array1 #Debug
return array1,array2,d otprod,sum,maxv alue #<<-- Not working as I thought it would.
x = Numeric.arange( 1,11) y = Numeric.arange( 1,11)*2 dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?
print 'Array X:',x print 'Array Y:',y abc(x,y,dotp,su mx,maxv) print 'Calling function abc' print 'Array X:',x print 'Array Y:',y print 'Dot Product of X and Y:',dotp print 'Sum of array X:',sumx print 'Max value of array Y:',maxv
If you run it the data gets passed to the function just fine and it finds the right numbers. Its just getting it to pass them back thats not working. I put some print statements inside the function just to see how the data gets passed. So any ideas that would help me? If you want to see the fortran code just email me.
David
On 2005-05-14, Philippe C. Martin wrote: You're thinking you're passing the arguments as reference
That is the way Fortran handles them:
[...] Right now I'm taking a simple program I wrote in Fortran
Bernd
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]
David wrote: Hi I'm trying to teach myself python and so far to good, but I'm
having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in Fortran and trying to do it in Python. I got it to work, but now I'm trying to modularize it. My fortran program uses a subroutine and I was trying to do the same thing in Python. But I'm still new so I'm having trouble understanding what I'm doing wrong. Here is my code:
#! /usr/bin/python #This program takes two vectors and multiplies them #against a 10X10 array. It also then gives the dot product, #sum, and max value of the array.
import Numeric def abc(array1,arra y2,dotprod,sum, maxvalue): """Takes two arrays and performs predetermined calculations, Then returns the solutions back as the same array, along with the dot product, sum, and max value of the array.""" #TODO: Get this damn thing working!! print "Array 1 in:",array1 #Debug data = Numeric.zeros(( 10,10)) for i in range(10): for j in range(10): data[i,j] = (i+1)+(j+1)
e = Numeric.matrixm ultiply(data, array1) g = Numeric.matrixm ultiply(Numeric .transpose(data ),array2) array1 = e array2 = g dotprod = Numeric.dot(arr ay1,array2) sum = Numeric.sum(arr ay1) maxvalue = array2[Numeric.argmax( array2)] print "Array 1 out:",array1 #Debug
return array1,array2,d otprod,sum,maxv alue #<<-- Not working as I thought it would.
x = Numeric.arange( 1,11) y = Numeric.arange( 1,11)*2 dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?
print 'Array X:',x print 'Array Y:',y abc(x,y,dotp,su mx,maxv) print 'Calling function abc' print 'Array X:',x print 'Array Y:',y print 'Dot Product of X and Y:',dotp print 'Sum of array X:',sumx print 'Max value of array Y:',maxv
If you run it the data gets passed to the function just fine and it finds the right numbers. Its just getting it to pass them back thats not working. I put some print statements inside the function just to see how the data gets passed. So any ideas that would help me? If
you want to see the fortran code just email me.
David
Hello David,
welcome to Python!
Python always returns 'something' from a function or method if nothing
is specified or there is no return you get an implicit None.
You have created a function that accepts a few arguments and processes
them then returns there value.
The problem is that you **have not** used the return values ;)
example:
def funk(first,seco nd,third):
return first,second,th ird
This can be like this:
print funk("theses three args can be anything",2,"th ree")
If you need to use the return values you need to assign them to a
"name" so you have a handle on them.
Variables are don't exist in Python there are only objects and names(
hence namespaces ), but not everyone is so strict and you still see
mention of 'variables' when they mean 'names'. ##dotp,sumx,max v = 0,0,0 # not needed here print 'Array X:',x print 'Array Y:',y ## Notice we assign the return values to names so we can access it
later arr1,arr2,dotp, sumx,maxv = abc(x,y,0,0,0) print 'Calling function abc' print 'Array X:',arr1 print 'Array Y:',arr2 print 'Dot Product of X and Y:',dotp print 'Sum of array X:',sumx print 'Max value of array Y:',maxv
Or you could pack all return values into a tuple and access it thru
slices. ##dotp,sumx,max v = 0,0,0 # not needed here print 'Array X:',x print 'Array Y:',y abcinfo = abc(x,y,0,0,0) print 'Calling function abc' print 'Array X:',abcinfo[0] print 'Array Y:',abcinfo[1] print 'Dot Product of X and Y:',abcinfo[2] print 'Sum of array X:',abcinfo[3] print 'Max value of array Y:',abcinfo[4]
Or you could use a dictionary, or etc...
The possibilities are endless.
Be sure to study up on namespaces, it will ease your Python woes.
Namespaces are the most fundamental part of Python that new users don't
understand. Namespace mastery will take you far.
Just remember there are only two scopes local and global ;) http://docs.python.org
hth,
M.E.Farmer
(x,y,dotp,sumx, maxv) = abc(x,y,dotp,su mx,maxv)
This will only create a tuple in memory that has no name to reference
it by!
How would you access the returned value?
If you want a tuple you need to assign all the return vales to a single
name.
mytup = abc(x,y,dotp,su mx,maxv)
M.E.Farmer
M.E.Farmer wrote: (x,y,dotp,sumx, maxv) = abc(x,y,dotp,su mx,maxv) This will only create a tuple in memory that has no name to reference it by!
Huh? This binds the names "x", "y", "dotp", "sumx" and "maxv" to the
values returned by abc:
py> def abc(*args):
.... return args
....
py> (x,y,dotp,sumx, maxv) = abc(2,3,5,7,11)
py> x
2
py> y
3
py> dotp
5
py> sumx
7
py> maxv
11
Of course, the parentheses around x,y,dotp,sumx,m axv are unnecessary.
STeVe
Thank you very much. Tulpes are really neat now that I've looked at
them. So after just fixing it, I changed it up and I would like to
show it off now.
#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.
import Numeric
def abc(package):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array.
Data slots:
0 - First Array
1 - Second Array
2 - Dot product
3 - Sum
4 - Max Value"""
f = Numeric.zeros(( 10,10))
for i in range(10):
for j in range(10):
f[i,j] = (i+1)+(j+1)
e = Numeric.matrixm ultiply(f, package[0])
g = Numeric.matrixm ultiply(Numeric .transpose(f),p ackage[1])
package[0] = e
package[1] = g
package[2] = Numeric.dot(pac kage[0],package[1])
package[3] = Numeric.sum(pac kage[0])
package[4] = package[1][Numeric.argmax( package[1])]
return package
data = [Numeric.arange( 1,11),Numeric.a range(1,11)*2,0 ,0,0]
#data = [Array X, Array Y, Dot product, Sum, Max Value]
print 'Array X:',data[0]
print 'Array Y:',data[1]
data = abc(data)
print 'Calling function abc'
print 'Array X:',data[0]
print 'Array Y:',data[1]
print 'Dot Product of X and Y:',data[2]
print 'Sum of array X:',data[3]
print 'Max value of array Y:',data[4]
I think its just wonderful now, but if you got any other suggestions,
Please do tell. Thanks everyone.
David
Hmm don't know what happened. I guess the formatting got all chewed up. On 2005-05-14, M.E.Farmer wrote:
(x,y,dotp,sumx, maxv) = abc(x,y,dotp,su mx,maxv) This will only create a tuple in memory that has no name to reference it by!
Maybe. But it does not seem to hurt. And I am not sure the tupel _is_
really created in that situation.
How would you access the returned value? If you want a tuple you need to assign all the return vales to a single name.
We do not want the tuple.
Python 2.3.5 (#1, Apr 28 2005, 12:14:04)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r2,
ssp-3.4.3.20050110-0, pie-
on linux2
Type "help", "copyright" , "credits" or "license" for more information. def foo(): ... return 1,2,3 ... (a,b,c)=foo() print a,b,c
1 2 3
works.
Of course, you can omit the ():
a,b,c=foo() print a,b,c
1 2 3
That makes no difference.
Bernd
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: 2mc |
last post by:
Generally speaking, if one had a list (from regular Python) and an
array (from Numerical Python) that contained the same number of
elements, would a While loop or a For loop process them at the same
speed? Or, would the array process faster?
I'm new to Python, so my question may expose my ignorance. I
appreciate anyone's effort to help me understand.
Thanks. It is much appreciated.
|
by: Kenneth McDonald |
last post by:
For unfortunate reasons, I'm considering switching back to Win XP
(from OS X) as my "main" system. Windows has so many annoyances that
I can only compare it to driving in the Bay Area at rush hour (OS X
is like driving in Portland at rush hour--not as bad, but getting
there), but there are really only a couple of things that are really,
absolutely preventing me from making the switch. Number one is the
lack of a decent command line and...
|
by: bwaha |
last post by:
I've posted this question to comp.graphics.apps.gnuplot too but given that
this python group may also comprise a lot of gnuplot users, and is far more
active, I've posted this question here too. My apologies to those who read
this twice. I posted to cgag before I decided to post here with a more
meaningful subject.
Anyone had any success getting stdout from gnuplot on an ms windows system
(XP)? I'm trying to read mouse coordinates...
|
by: Harry |
last post by:
Hi All,
It is nice to join the python group. Can someone please help me with
a python question?
I have the following object which is like a list of tuples
What command do I use to get the value corresponding to 'min'?
This object seems to be non-indexable
row=
|
by: Hoop |
last post by:
Hi,
I have been working in getting Boost.Python running on my PC, seems to
work now.
I have what I believe is somewhat of basic question here.
I am starting on an application that will developed in VS2005, probably
using C++/CLI.
I want to be able to exchange data in between Python and C++. The user
when running the C++ app will be able to call a python script, set
some values, that will then be communicated to running application, it
| |
by: Scheol Service |
last post by:
---------- Forwarded message ----------
From: Scheol Service <scheols@gmail.com>
Date: Nov 29, 2006 10:57 PM
Subject: Python Question About Compiling.
To: mailto:python-list@python.org
Im just unsure on how to compile python code into .exe executionable
files. Is there a simple way to do this?
|
by: Stodge |
last post by:
Hi folks, new to Boost Python and struggling to build a prototype at
work. I thought I'd start with a conceptual question to help clarify
my understanding. I already have a basic prototype working nicely but
I'm having a few issues, which I may post about later.
A brief functional rundown of what I'm trying to prototype. Hopefully
my explanation doesn't get too confusing!
I'm embedding a python module into an application; Python will...
|
by: Thomas Troeger |
last post by:
Dear all,
I've successfully embedded the Python interpreter into a set of C/C++
application programs that use a larger library project with information
from http://docs.python.org/api/api.html and
http://docs.python.org/ext/ext.html. Now I want to wrap classes and
functions from the associated libraries so that I can write new
applications completely in Python, but I'm not entirely sure how to
start because I have some problems...
|
by: |
last post by:
I can't seem to figure this out. I just installed Python 2.5.2 a few days ago on my OS X 10.4.11
system. It runs fine and if I type "Python -V" in the Terminal it outputs "Python 2.5.2" which is
correct. However, if I try to run a 'do shell script' in AppleScript which I'm wanting to run a Python
program, it reverts to using Python 2.3. For example, if I run this code in AppleScript:
--------------------
set p to "#!/usr/bin/python...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |