Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

UCALC equivalent

2 views
Skip to first unread message

Dark Cowherd

unread,
Aug 12, 2005, 1:17:01 PM8/12/05
to python
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?


--
DarkCowherd

Larry Bates

unread,
Aug 12, 2005, 1:30:15 PM8/12/05
to Dark Cowherd, python
Python has built in eval function and doesn't require
a library.

Larry Bates

Larry Bates

unread,
Aug 12, 2005, 1:30:15 PM8/12/05
to Dark Cowherd, python
Python has built in eval function and doesn't require
a library.

Larry Bates

max

unread,
Aug 12, 2005, 1:49:44 PM8/12/05
to
Larry Bates <lba...@syscononline.com> wrote in
news:42FCDCA7...@syscononline.com:

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

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

Scott David Daniels

unread,
Aug 12, 2005, 3:18:11 PM8/12/05
to
max wrote:
> Larry Bates <lba...@syscononline.com> wrote in
> news:42FCDCA7...@syscononline.com:
>>Python has built in eval function and doesn't require a library.
>
> 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.

He is absolutely correct.
From the web page referenced:

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

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


The python equivalent:

exec "def area(length,width): 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....@Acm.Org

Max Erickson

unread,
Aug 12, 2005, 7:03:00 PM8/12/05
to
Scott David Daniels <Scott....@Acm.Org> wrote in
news:42fcea4a$1...@nntp0.pdx.net:

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

Scott David Daniels

unread,
Aug 12, 2005, 8:06:50 PM8/12/05
to
Max Erickson wrote:
> Scott David Daniels <Scott....@Acm.Org> wrote in
> news:42fcea4a$1...@nntp0.pdx.net:
>
>
>>max wrote:
>> From the web page referenced:
>> ...

>> cout << ucEval("abc(5)-abc(3,4)*(#b01101 shl 1)")
>> << endl;
>>The python equivalent:
>> ...

>> print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")
>
> Ouch, I sure was wrong. You did such a good job making me look
> foolish that it was mentioned in Python-URL!.
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....@Acm.Org

Cameron Laird

unread,
Aug 12, 2005, 8:08:04 PM8/12/05
to
In article <Xns96B0C211F72E8m...@216.168.3.44>,
Max Erickson <maxer...@gmail.com> wrote:
.
.
.

>> The python equivalent:
>>
>> exec "def area(length,width): 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....@Acm.Org
>>
>
>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

"Python-URL!" makes a point of highlighting positive achievements.
We all create enough foolishness to obviate any need to pass *that*
on.

Dark Cowherd

unread,
Aug 12, 2005, 10:41:58 PM8/12/05
to python
thx,

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

--
DarkCowherd

William Park

unread,
Aug 13, 2005, 12:30:14 AM8/13/05
to

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
http://home.eol.ca/~parkw/index.html#rpn
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 <openge...@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/

John Machin

unread,
Aug 13, 2005, 12:45:33 AM8/13/05
to

Perhaps this should be:
exec "def shl(x, y): return x * 2 ** y"
or
exec "def shl(x, y): return x << y"

Scott David Daniels

unread,
Aug 13, 2005, 10:55:48 AM8/13/05
to
John Machin wrote:
> Scott David Daniels wrote:
>> max wrote:
>>> Larry Bates <lba...@syscononline.com> wrote in
>> The python equivalent:
>> ...

>> exec "def shl(x, y): return x * 2^y"
>
> Perhaps this should be:
> exec "def shl(x, y): return x * 2 ** y"
> or
> exec "def shl(x, y): return x << y"
Exactly.
Demonstrating the problems of not actually running a test case
with known results.

--Scott David Daniels
Scott....@Acm.Org

0 new messages