UCALC equivalent

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

    #1

    UCALC equivalent



    There is a great library called UCALC which allows you to set up an
    expression and evaluate it
    for e.g. you an define an expression by calling a function in UCALC
    then call it with various values of x

    for e.g. see this page



    It is very fast. I have used it in VB when there is lot of number
    crunching to be done.
    Is there a Python equivalent.

    I looked at numPy and SciPy sites (just skimmed through) did'nt seem
    to have what I wanted.

    Any pointers?


    --
    DarkCowherd
  • Larry Bates

    #2
    Re: UCALC equivalent

    Python has built in eval function and doesn't require
    a library.

    Larry Bates

    Dark Cowherd wrote:[color=blue]
    > http://www.ucalc.com/mathparser/index.html
    >
    > There is a great library called UCALC which allows you to set up an
    > expression and evaluate it
    > for e.g. you an define an expression by calling a function in UCALC
    > then call it with various values of x
    >
    > for e.g. see this page
    > http://www.ucalc.com/mathparser/sample6.html
    >
    >
    > It is very fast. I have used it in VB when there is lot of number
    > crunching to be done.
    > Is there a Python equivalent.
    >
    > I looked at numPy and SciPy sites (just skimmed through) did'nt seem
    > to have what I wanted.
    >
    > Any pointers?
    >
    >[/color]

    Comment

    • Larry Bates

      #3
      Re: UCALC equivalent

      Python has built in eval function and doesn't require
      a library.

      Larry Bates

      Dark Cowherd wrote:[color=blue]
      > http://www.ucalc.com/mathparser/index.html
      >
      > There is a great library called UCALC which allows you to set up an
      > expression and evaluate it
      > for e.g. you an define an expression by calling a function in UCALC
      > then call it with various values of x
      >
      > for e.g. see this page
      > http://www.ucalc.com/mathparser/sample6.html
      >
      >
      > It is very fast. I have used it in VB when there is lot of number
      > crunching to be done.
      > Is there a Python equivalent.
      >
      > I looked at numPy and SciPy sites (just skimmed through) did'nt seem
      > to have what I wanted.
      >
      > Any pointers?
      >
      >[/color]

      Comment

      • max

        #4
        Re: UCALC equivalent

        Larry Bates <lbates@syscono nline.com> wrote in
        news:42FCDCA7.7 030107@sysconon line.com:
        [color=blue]
        > Python has built in eval function and doesn't require
        > a library.
        >
        > Larry Bates
        >[/color]

        Are you kidding? Read the original post a little more closely. The
        o.p. is looking for a library that evaluates mathematical expressions
        and is callable from python code.

        max

        Comment

        • Scott David Daniels

          #5
          Re: UCALC equivalent

          max wrote:[color=blue]
          > Larry Bates <lbates@syscono nline.com> wrote in
          > news:42FCDCA7.7 030107@sysconon line.com:[color=green]
          >>Python has built in eval function and doesn't require a library.[/color]
          >
          > Are you kidding? Read the original post a little more closely. The
          > o.p. is looking for a library that evaluates mathematical expressions
          > and is callable from python code.[/color]

          He is absolutely correct.
          From the web page referenced:

          ucDefineFunctio n("area(length, width) = length*width");
          ucDefineFunctio n("frac(x)=abs( abs(x)-int(abs(x)))");
          ucDefineFunctio n("test() = 5");
          ucDefineFunctio n("abc(x, y=10) = x + y");
          ucDefineFunctio n("shl[x, y] = x * 2^y");

          cout.precision( 16);
          cout << ucEval("frac(15 0/17) * area(20,30)") << endl;
          cout << ucEval("abc(5)-abc(3,4)*(#b011 01 shl 1)")
          << endl;


          The python equivalent:

          exec "def area(length,wid th): return length*width"
          exec "def frac(x): return abs(abs(x) - int(abs(x)))"
          exec "def test(): return 5"
          exec "def abc(x, y=10): return x + y"
          exec "def shl(x, y): return x * 2^y"

          print eval("frac(150/17) * area(20,30)")
          print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")

          --Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          • Max Erickson

            #6
            Re: UCALC equivalent

            Scott David Daniels <Scott.Daniels@ Acm.Org> wrote in
            news:42fcea4a$1 @nntp0.pdx.net:
            [color=blue]
            > max wrote:[color=green]
            >> Larry Bates <lbates@syscono nline.com> wrote in
            >> news:42FCDCA7.7 030107@sysconon line.com:[color=darkred]
            >>>Python has built in eval function and doesn't require a library.[/color]
            >>
            >> Are you kidding? Read the original post a little more closely.
            >> The o.p. is looking for a library that evaluates mathematical
            >> expressions and is callable from python code.[/color]
            >
            > He is absolutely correct.
            > From the web page referenced:
            >
            > ucDefineFunctio n("area(length, width) = length*width");
            > ucDefineFunctio n("frac(x)=abs( abs(x)-int(abs(x)))");
            > ucDefineFunctio n("test() = 5");
            > ucDefineFunctio n("abc(x, y=10) = x + y");
            > ucDefineFunctio n("shl[x, y] = x * 2^y");
            >
            > cout.precision( 16);
            > cout << ucEval("frac(15 0/17) * area(20,30)") << endl;
            > cout << ucEval("abc(5)-abc(3,4)*(#b011 01 shl 1)")
            > << endl;
            >
            >
            > The python equivalent:
            >
            > exec "def area(length,wid th): return length*width"
            > exec "def frac(x): return abs(abs(x) - int(abs(x)))"
            > exec "def test(): return 5"
            > exec "def abc(x, y=10): return x + y"
            > exec "def shl(x, y): return x * 2^y"
            >
            > print eval("frac(150/17) * area(20,30)")
            > print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")
            >
            > --Scott David Daniels
            > Scott.Daniels@A cm.Org
            >[/color]

            Ouch, I sure was wrong. You did such a good job making me look
            foolish that it was mentioned in Python-URL!. At least Larry Bates
            had the grace not to call me an idiot.

            max

            Comment

            • Scott David Daniels

              #7
              Re: UCALC equivalent

              Max Erickson wrote:[color=blue]
              > Scott David Daniels <Scott.Daniels@ Acm.Org> wrote in
              > news:42fcea4a$1 @nntp0.pdx.net:
              >
              >[color=green]
              >>max wrote:
              >> From the web page referenced:
              >> ...
              >> cout << ucEval("abc(5)-abc(3,4)*(#b011 01 shl 1)")
              >> << endl;
              >>The python equivalent:
              >> ...
              >> print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")[/color]
              >
              > Ouch, I sure was wrong. You did such a good job making me look
              > foolish that it was mentioned in Python-URL!.[/color]
              Sorry, I wasn't trying to make you look foolish. I was trying to
              nip a misconception in the bud.

              I also forgot how to convert binary to an integer (the #b01101 above)
              when I was writing it, so that last line should read:
              print eval("abc(5) - abc(3,4) * shl(int('01101' , 2), 1)")


              --Scott David Daniels
              Scott.Daniels@A cm.Org

              Comment

              • Cameron Laird

                #8
                Re: UCALC equivalent

                In article <Xns96B0C211F72 E8maxericksongm ailcom@216.168. 3.44>,
                Max Erickson <maxerickson@gm ail.com> wrote:

                Comment

                • Dark Cowherd

                  #9
                  Re: UCALC equivalent

                  thx,

                  Well moving to Python from another language needs lots of chanegs
                  inside your head.

                  --
                  DarkCowherd

                  Comment

                  • William Park

                    #10
                    Re: UCALC equivalent

                    Dark Cowherd <darkcowherd@gm ail.com> wrote:[color=blue]
                    > http://www.ucalc.com/mathparser/index.html
                    >
                    > There is a great library called UCALC which allows you to set up an
                    > expression and evaluate it
                    > for e.g. you an define an expression by calling a function in UCALC
                    > then call it with various values of x
                    >
                    > for e.g. see this page
                    > http://www.ucalc.com/mathparser/sample6.html
                    >
                    > It is very fast. I have used it in VB when there is lot of number
                    > crunching to be done.
                    > Is there a Python equivalent.
                    >
                    > I looked at numPy and SciPy sites (just skimmed through) did'nt seem
                    > to have what I wanted.
                    >
                    > Any pointers?[/color]

                    Python has 'eval' and 'exec', so you can process at run-time anything
                    that you can type into script file. However, it will be a bit more
                    verbose than UCALC, because Python is more general.

                    If you're looking for scientific calculator, which can be embedded or
                    scripted, then perhaps you should take a look at

                    It is RPN calculator with full support for <math.h> functions and some
                    features found in typical programmable scientific calculators.

                    Eg.
                    3+4/5-8 --> rpn 4 5 / 3 + 8 - = --> -4.2

                    x^2+5x-10
                    --> rpn 'f(x)= dup 5 + x 10 -'
                    rpn 1 'f(x)' = --> -4
                    rpn 5 'f(x)' = --> 40
                    or it can be scripted directly using shell function, like
                    --> func () {
                    rpn $1 dup 5 + x 10 - =
                    }
                    func 1
                    func 5

                    Sum(x^2+5, 1, 10). I assume this is sum of x^2+5, for x=1,2,...,10
                    --> rpn clear
                    for i in {1..10}; do
                    rpn $i x^2 5 + +
                    done
                    rpn = --> 435

                    --
                    William Park <opengeometry@y ahoo.ca>, Toronto, Canada
                    ThinFlash: Linux thin-client on USB key (flash) drive

                    BashDiff: Super Bash shell
                    Compare the best free open source Software Development Software at SourceForge. Free, secure and fast Software Development Software downloads from the largest Open Source applications and software directory

                    Comment

                    • John Machin

                      #11
                      Re: UCALC equivalent

                      Scott David Daniels wrote:[color=blue]
                      > max wrote:
                      >[color=green]
                      >> Larry Bates <lbates@syscono nline.com> wrote in
                      >> news:42FCDCA7.7 030107@sysconon line.com:
                      >>[color=darkred]
                      >>> Python has built in eval function and doesn't require a library.[/color]
                      >>
                      >>
                      >> Are you kidding? Read the original post a little more closely. The
                      >> o.p. is looking for a library that evaluates mathematical expressions
                      >> and is callable from python code.[/color]
                      >
                      >
                      > He is absolutely correct.
                      > From the web page referenced:
                      >
                      > ucDefineFunctio n("area(length, width) = length*width");
                      > ucDefineFunctio n("frac(x)=abs( abs(x)-int(abs(x)))");
                      > ucDefineFunctio n("test() = 5");
                      > ucDefineFunctio n("abc(x, y=10) = x + y");
                      > ucDefineFunctio n("shl[x, y] = x * 2^y");
                      >
                      > cout.precision( 16);
                      > cout << ucEval("frac(15 0/17) * area(20,30)") << endl;
                      > cout << ucEval("abc(5)-abc(3,4)*(#b011 01 shl 1)")
                      > << endl;
                      >
                      >
                      > The python equivalent:
                      >
                      > exec "def area(length,wid th): return length*width"
                      > exec "def frac(x): return abs(abs(x) - int(abs(x)))"
                      > exec "def test(): return 5"
                      > exec "def abc(x, y=10): return x + y"
                      > exec "def shl(x, y): return x * 2^y"[/color]

                      Perhaps this should be:
                      exec "def shl(x, y): return x * 2 ** y"
                      or
                      exec "def shl(x, y): return x << y"
                      [color=blue]
                      >
                      > print eval("frac(150/17) * area(20,30)")
                      > print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")
                      >
                      > --Scott David Daniels
                      > Scott.Daniels@A cm.Org[/color]

                      Comment

                      • Scott David Daniels

                        #12
                        Re: UCALC equivalent

                        John Machin wrote:[color=blue]
                        > Scott David Daniels wrote:[color=green]
                        >> max wrote:[color=darkred]
                        >>> Larry Bates <lbates@syscono nline.com> wrote in[/color]
                        >> The python equivalent:
                        >> ...
                        >> exec "def shl(x, y): return x * 2^y"[/color]
                        >
                        > Perhaps this should be:
                        > exec "def shl(x, y): return x * 2 ** y"
                        > or
                        > exec "def shl(x, y): return x << y"[/color]
                        Exactly.
                        Demonstrating the problems of not actually running a test case
                        with known results.

                        --Scott David Daniels
                        Scott.Daniels@A cm.Org

                        Comment

                        Working...