Help me on function definition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aeneng

    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__=="__ma in__":
    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__


  • Steven D'Aprano

    #2
    Re: Help me on function definition

    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

    Comment

    • Steven D'Aprano

      #3
      Re: Help me on function definition

      Oh, I forgot to mention...

      On Sat, 29 Mar 2008 01:47:21 +0000, aeneng wrote:
      if __name__=="__ma in__":
      ....
      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

      Comment

      • John Machin

        #4
        Re: Help me on function definition

        On Mar 29, 12:47 pm, "aeneng" <aen...@yahoo.c omwrote:
        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__=="__ma in__":
        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__

        Comment

        • Roel Schroeven

          #5
          Re: Help me on function definition

          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

          Comment

          • castironpi@gmail.com

            #6
            Re: Help me on function definition

            On Mar 29, 4:18 am, Roel Schroeven <rschroev_nospa m...@fastmail.f m>
            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.

            Comment

            • hdante

              #7
              Re: Help me on function definition

              On Mar 28, 10:47 pm, "aeneng" <aen...@yahoo.c omwrote:
              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:

              Why SciPy? Fundamental algorithms. Broadly applicable. Foundational. Interoperable. Performant. Open source.


              (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__=="__ma in__":
              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__

              Comment

              • castironpi@gmail.com

                #8
                Re: Help me on function definition

                On Mar 29, 11:45 pm, hdante <hda...@gmail.c omwrote:
                On Mar 28, 10:47 pm, "aeneng" <aen...@yahoo.c omwrote:
                >
                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__=="__ma in__":
                        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()

                Comment

                • Alan Isaac

                  #9
                  Re: Help me on function definition

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


                  Missing colon.



                  hth,

                  Alan Isaac


                  Comment

                  Working...