473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help me on function definition

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
8 1751
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
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
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
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
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
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
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
aeneng wrote:
WHAT IS WRONG WITH MY CODE?
def cross(u,v)


Missing colon.

hth,

Alan Isaac
Apr 5 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dennis | last post by:
Hi, This may be a simple answer for somebody, but I'm unable to figure it out. In a nutshell: I have a base class defined. Let's call it XClient. Inside XClient I have a virtual method...
0
by: Ben Harper | last post by:
I am stuck using VC7 right now, so I can't test this on 7.1, but I believe it is a bug in the compiler, although I hope it isn't. Please could someone confirm this for me, prove me wrong, or suggest...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
10
by: B Williams | last post by:
I have been working with this code for a better part of the day and I can't figure out where I am making a mistake. I can only imagine it is when I declare multiple paramaters on the constructor...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; int main() { extern "C" int f(int, int);
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
14
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
3
by: hjast | last post by:
I am doing an assingment for class and I have everything execpt it will not compile. I get these errors /Users/hjast89/Desktop/test/shape.cpp:36: error: expected `}' at end of input...
6
by: djm | last post by:
hello everyone, im doing a c++ coursework which consists linked lists and use of classes. but im getting some compilation errors. can some please help me out. //this is the header file...
7
by: CWilliam912 | last post by:
C++ Programming From Problem analysis to program design Third Edition D.S. Malik Consider the following function main: int main () { int inStock ; int alpha ; int beta ;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.