By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Help me on function definition

P: n/a
Hello everyone,

I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.

when I import the function, error message show like this
>>import cross
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "cross.py", line 8
ppp2=u[2]*v[0]-u[0]*v[2]
^
SyntaxError: invalid syntax

WHAT IS WRONG WITH MY CODE?

I appreciate your help.

##here is the function definition.
##cross.py##
def cross(u,v)
"""input two vectors u and v in 3-D space,
output a cross product of vector w, in column or in row
accordingly."""
ppp1,ppp2,ppp3=0.0,0.0,0.0
ppp1=u[1]*v[2]-u[2]*v[1]
ppp2=u[2]*v[0]-u[0]*v[2]
ppp3=u[0]*v[1]-u[1]*v[0]
# store the result of the cross product in u
u[0]=ppp1
u[1]=ppp2
u[2]=ppp3
return u #return the cross product of vector u x v.
if __name__=="__main__":
from cvxopt.base import matrix
u=matrix([1.0,0.0,0.0],(3,1))
v=matrix([0.0,1.0,0.0],(3,1))
print cross(u,v)
print "file name is %s" %__name__
Mar 29 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Sat, 29 Mar 2008 01:47:21 +0000, aeneng wrote:
Hello everyone,

I am just starting to use python in numerical cacluation. I need you to
help me to see what's wrong with the following piece of codes, which
computes the cross product of two vectors and returns the result. u and
v are two 3x1 matrix.

when I import the function, error message show like this
>>>import cross
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "cross.py", line 8
ppp2=u[2]*v[0]-u[0]*v[2]
^
SyntaxError: invalid syntax

WHAT IS WRONG WITH MY CODE?

You don't win any points by writing the most unreadable code can you. A
little bit of white space makes the code so much easier to read:

ppp2 = u[2]*v[0] - u[0]*v[2]
But that's not the problem.

Your code mixes spaces and tabs for indentation. Spaces are good; tabs
are good; both together will eventually lead to disaster.

But that's not your problem either.

Your REAL problem is that the error you are reporting is NOT the error
your code gives. Hint: if your code raises an error, you must copy the
code that actually raises an error, not different code with different
errors.
The code you post reports this error:

>>import cross
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cross.py", line 1
def cross(u,v)
^
SyntaxError: invalid syntax
After fixing that fault (put a colon after the function definition), your
code imports correctly.

My *guess* is that in your actual code that fails, you have forgotten to
close a bracket or brace in line 7 (not 8).

--
Steven
Mar 29 '08 #2

P: n/a
Oh, I forgot to mention...

On Sat, 29 Mar 2008 01:47:21 +0000, aeneng wrote:
if __name__=="__main__":
....
print "file name is %s" %__name__

This doesn't do what you expect it to do. You've already established that
__name__ is equal to "__main__", so you might as well change that line to:

print "file name is not actually __main__"

What you want is:

print "file name is %s" % __file__

--
Steven
Mar 29 '08 #3

P: n/a
On Mar 29, 12:47 pm, "aeneng" <aen...@yahoo.comwrote:
Hello everyone,

I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.

when I import the function, error message show like this>>import cross

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "cross.py", line 8
ppp2=u[2]*v[0]-u[0]*v[2]
^
SyntaxError: invalid syntax

WHAT IS WRONG WITH MY CODE?

I appreciate your help.

##here is the function definition.
##cross.py##
def cross(u,v)
"""input two vectors u and v in 3-D space,
output a cross product of vector w, in column or in row
accordingly."""
ppp1,ppp2,ppp3=0.0,0.0,0.0
ppp1=u[1]*v[2]-u[2]*v[1]
ppp2=u[2]*v[0]-u[0]*v[2]
ppp3=u[0]*v[1]-u[1]*v[0]
# store the result of the cross product in u
u[0]=ppp1
u[1]=ppp2
u[2]=ppp3
return u #return the cross product of vector u x v.
And you are stuffing the result into the first argument as well as
returning the result ... not a good idea.
if __name__=="__main__":
from cvxopt.base import matrix
u=matrix([1.0,0.0,0.0],(3,1))
v=matrix([0.0,1.0,0.0],(3,1))
print cross(u,v)
print "file name is %s" %__name__
Mar 29 '08 #4

P: n/a
aeneng schreef:
Hello everyone,

I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.

when I import the function, error message show like this
>>>import cross
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "cross.py", line 8
ppp2=u[2]*v[0]-u[0]*v[2]
^
SyntaxError: invalid syntax

WHAT IS WRONG WITH MY CODE?

I appreciate your help.

##here is the function definition.
##cross.py##
def cross(u,v)
"""input two vectors u and v in 3-D space,
output a cross product of vector w, in column or in row
accordingly."""
ppp1,ppp2,ppp3=0.0,0.0,0.0
You forgot the : after def cross(u, v)

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
Mar 29 '08 #5

P: n/a
On Mar 29, 4:18*am, Roel Schroeven <rschroev_nospam...@fastmail.fm>
wrote:
aeneng schreef:


Hello everyone,
I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.
when I import the function, error message show like this
>>import cross
Traceback (most recent call last):
* File "<stdin>", line 1, in ?
* File "cross.py", line 8
* * ppp2=u[2]*v[0]-u[0]*v[2]
* * ^
SyntaxError: invalid syntax
WHAT IS WRONG WITH MY CODE?
I appreciate your help.
##here is the function definition.
##cross.py##
def cross(u,v)
* * """input two vectors u and v in 3-D space, * *
* * * *output a cross product of vector w, in column or in row
accordingly."""
* * ppp1,ppp2,ppp3=0.0,0.0,0.0

You forgot the : after def cross(u, v)
No; directional is only signed and unsigned. </tangent>

There's a laptop on my computer.
Mar 29 '08 #6

P: n/a
On Mar 28, 10:47 pm, "aeneng" <aen...@yahoo.comwrote:
Hello everyone,
Hi,

Always avoid reinventing the wheel:

from Numeric import array, cross_product
a = array([1, 2, 3])
b = array([4, 5, 6])
print cross_product(a, b)

See:
http://numpy.scipy.org/
http://www.scipy.org/

(hint: consider that many people want to multiply matrices) :-)
>
I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.

when I import the function, error message show like this>>import cross

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "cross.py", line 8
ppp2=u[2]*v[0]-u[0]*v[2]
^
SyntaxError: invalid syntax

WHAT IS WRONG WITH MY CODE?

I appreciate your help.

##here is the function definition.
##cross.py##
def cross(u,v)
"""input two vectors u and v in 3-D space,
output a cross product of vector w, in column or in row
accordingly."""
ppp1,ppp2,ppp3=0.0,0.0,0.0
ppp1=u[1]*v[2]-u[2]*v[1]
ppp2=u[2]*v[0]-u[0]*v[2]
ppp3=u[0]*v[1]-u[1]*v[0]
# store the result of the cross product in u
u[0]=ppp1
u[1]=ppp2
u[2]=ppp3
return u #return the cross product of vector u x v.
if __name__=="__main__":
from cvxopt.base import matrix
u=matrix([1.0,0.0,0.0],(3,1))
v=matrix([0.0,1.0,0.0],(3,1))
print cross(u,v)
print "file name is %s" %__name__
Mar 30 '08 #7

P: n/a
On Mar 29, 11:45*pm, hdante <hda...@gmail.comwrote:
On Mar 28, 10:47 pm, "aeneng" <aen...@yahoo.comwrote:
Hello everyone,

*Hi,

*Always avoid reinventing the wheel:

from Numeric import array, cross_product
a = array([1, 2, 3])
b = array([4, 5, 6])
print cross_product(a, b)

*See:
*http://numpy.scipy.org/
*http://www.scipy.org/

*(hint: consider that many people want to multiply matrices) :-)


I am just starting to use python in numerical cacluation.
I need you to help me to see what's wrong with the following piece of
codes, which computes the cross product of two vectors and returns
the result. u and v are two 3x1 matrix.
when I import the function, error message show like this>>import cross
Traceback (most recent call last):
* File "<stdin>", line 1, in ?
* File "cross.py", line 8
* * ppp2=u[2]*v[0]-u[0]*v[2]
* * ^
SyntaxError: invalid syntax
WHAT IS WRONG WITH MY CODE?
I appreciate your help.
##here is the function definition.
##cross.py##
def cross(u,v)
* * """input two vectors u and v in 3-D space,
* * * *output a cross product of vector w, in column or in row
accordingly."""
* * ppp1,ppp2,ppp3=0.0,0.0,0.0
* * ppp1=u[1]*v[2]-u[2]*v[1]
* * ppp2=u[2]*v[0]-u[0]*v[2]
* * ppp3=u[0]*v[1]-u[1]*v[0]
* * # store the result of the cross product in u
* * u[0]=ppp1
* * u[1]=ppp2
* * u[2]=ppp3
* * return u #return the cross product of vector u x v.
if __name__=="__main__":
* * * * from cvxopt.base import matrix
* * * * u=matrix([1.0,0.0,0.0],(3,1))
* * * * v=matrix([0.0,1.0,0.0],(3,1))
* * * * print cross(u,v)
* * * * print "file name is %s" %__name__- Hide quoted text -
Bandwidth is really low; offload logic. I'd run some AI for the games
so you can get a couple more triangles... plot.thicken()
Mar 30 '08 #8

P: n/a
aeneng wrote:
WHAT IS WRONG WITH MY CODE?
def cross(u,v)


Missing colon.

hth,

Alan Isaac
Apr 5 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.