Thread: [pure-lang-svn] SF.net SVN: pure-lang: [329] pure/trunk/lib/math.pure
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-06-28 18:51:19
|
Revision: 329
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=329&view=rev
Author: agraef
Date: 2008-06-28 11:51:25 -0700 (Sat, 28 Jun 2008)
Log Message:
-----------
Add math.pure library module.
Added Paths:
-----------
pure/trunk/lib/math.pure
Added: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure (rev 0)
+++ pure/trunk/lib/math.pure 2008-06-28 18:51:25 UTC (rev 329)
@@ -0,0 +1,93 @@
+
+/* Pure basic math routines. */
+
+/* Copyright (c) 2008 by Albert Graef <Dr....@t-...>.
+
+ This file is part of the Pure programming language and system.
+
+ Pure is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Pure is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* IEEE floating point infinities and NaNs. */
+
+def inf = 1.0e307 * 1.0e307; def nan = inf-inf;
+
+/* Absolute value and sign of a number. */
+
+abs x::int | abs x::bigint | abs x::double
+ = if x>=0 then x else -x;
+sgn x::int | sgn x::bigint | sgn x::double
+ = if x>0 then 1 else if x<0 then -1 else 0;
+
+/* Generic min and max functions. */
+
+min x y = if x<=y then x else y;
+max x y = if x>=y then x else y;
+
+/* Generic succ and pred functions. */
+
+succ x = x+1;
+pred x = x-1;
+
+/* Floor and ceil functions. */
+
+extern double floor(double), double ceil(double);
+
+floor x::int | floor x::bigint = x;
+ceil x::int | ceil x::bigint = x;
+
+/* Trigonometric functions. */
+
+extern double sin(double), double cos(double), double tan(double);
+extern double asin(double), double acos(double), double atan(double);
+extern double atan2(double,double);
+
+sin x::int | sin x::bigint = sin (double x);
+cos x::int | cos x::bigint = cos (double x);
+tan x::int | tan x::bigint = tan (double x);
+
+asin x::int | asin x::bigint = asin (double x);
+acos x::int | acos x::bigint = acos (double x);
+atan x::int | atan x::bigint = atan (double x);
+
+atan2 x::int y::int | atan2 x::bigint y::bigint |
+atan2 x::bigint y::int | atan2 x::int y::bigint = atan2 (double x) (double y);
+atan2 x::int y::double | atan2 x::bigint y::double = atan2 (double x) y;
+atan2 x::double y::int | atan2 x::double y::bigint = atan2 x (double y);
+
+/* Exponential function and logarithms. */
+
+extern double exp(double), double log(double) = c_log;
+
+ln x::double = c_log x if x>=0.0;
+log x::double = c_log x/c_log 10.0 if x>=0.0;
+
+exp x::int | exp x::bigint = exp (double x);
+ln x::int | ln x::bigint = ln (double x);
+log x::int | log x::bigint = log (double x);
+
+/* Hyperbolic functions. */
+
+extern double sinh(double), double cosh(double), double tanh(double);
+extern double asinh(double);
+extern double acosh(double) = c_acosh, double atanh(double) = c_atanh;
+
+acosh x::double = c_acosh x if x>=1.0;
+atanh x::double = c_atanh x if abs x<=1.0;
+
+sinh x::int | sinh x::bigint = sinh (double x);
+cosh x::int | cosh x::bigint = cosh (double x);
+tanh x::int | tanh x::bigint = tanh (double x);
+asinh x::int | asinh x::bigint = asinh (double x);
+acosh x::int | acosh x::bigint = acosh (double x);
+atanh x::int | atanh x::bigint = atanh (double x);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-06-28 20:05:25
|
Revision: 331
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=331&view=rev
Author: agraef
Date: 2008-06-28 13:05:34 -0700 (Sat, 28 Jun 2008)
Log Message:
-----------
Add pi constant.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-06-28 19:48:33 UTC (rev 330)
+++ pure/trunk/lib/math.pure 2008-06-28 20:05:34 UTC (rev 331)
@@ -65,6 +65,8 @@
atan2 x::int y::double | atan2 x::bigint y::double = atan2 (double x) y;
atan2 x::double y::int | atan2 x::double y::bigint = atan2 x (double y);
+def pi = 4.0*atan 1.0;
+
/* Exponential function and logarithms. */
extern double exp(double), double log(double) = c_log;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 10:42:58
|
Revision: 346
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=346&view=rev
Author: agraef
Date: 2008-07-01 03:43:04 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Add complex numbers.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-06-30 20:00:55 UTC (rev 345)
+++ pure/trunk/lib/math.pure 2008-07-01 10:43:04 UTC (rev 346)
@@ -1,5 +1,5 @@
-/* Pure basic math routines. */
+/* Pure math routines. Also defines the complex numbers. */
/* Copyright (c) 2008 by Albert Graef <Dr....@t-...>.
@@ -93,3 +93,262 @@
asinh x::int | asinh x::bigint = asinh (double x);
acosh x::int | acosh x::bigint = acosh (double x);
atanh x::int | atanh x::bigint = atanh (double x);
+
+/* Complex numbers. We provide both rectangular (x+:y) and polar (r<:a)
+ representations, where (x,y) are the Cartesian coordinates and (r,t) the
+ radius (absolute value) and angle (in radians) of a complex number,
+ respectively. The +: and <: constructors bind weaker than all other
+ arithmetic operators and are non-associative. */
+
+infix 5 +: <: ;
+
+/* Constructor equations to normalize the polar representation r<:t so that r
+ is always nonnegative and t falls in the range -pi<t<=pi. */
+
+r::int<:t |
+r::bigint<:t |
+r::double<:t = -r <: t+pi if r<0;
+
+r<:t::int |
+r<:t::bigint |
+r<:t::double = r <: atan2 (sin t) (cos t) if t<-pi || t>pi;
+ = r <: pi if t==-pi;
+
+/* The imaginary unit. */
+
+def i = 0+:1;
+
+/* The following operations all work with both the rectangular and the polar
+ representation, promoting real inputs to complex where appropriate. When
+ the result of an operation is again a complex number, it generally uses the
+ same representation as the input (except for explicit conversions). */
+
+/* We mostly follow Bronstein/Semendjajew here. Please mail me if you know
+ better formulas for any of these. */
+
+/* Convert any kind of number to a complex value. */
+
+complex z@(x+:y) | complex z@(r<:t) = z;
+complex x::int | complex x::bigint = x+:0;
+complex x::double = x+:0.0;
+
+/* Convert between polar and rectangular representations. */
+
+polar (x+:y) = sqrt (x*x+y*y) <: atan2 y x;
+rect (r<:t) = r*cos t +: r*sin t;
+
+/* For convenience, make these work with real values, too. */
+
+polar x::int | polar x::bigint = x<:0;
+polar x::double = x<:0.0;
+
+rect x::int | rect x::bigint = x+:0;
+rect x::double = x+:0.0;
+
+/* Create complex values on the unit circle. Note: To quickly compute
+ exp (x+:y) in polar form, use exp x*cis y. */
+
+cis t = rect (1<:t);
+
+/* Modulus (absolute value) and argument (angle). Note that you can also find
+ both of these in one go by converting to polar form. */
+
+abs (x+:y) = sqrt (x*x+y*y);
+abs (r<:t) = r;
+
+arg (x+:y) = atan2 y x;
+arg (r<:t) = t;
+
+arg x::int |
+arg x::bigint |
+arg x::double = atan2 0 x;
+
+/* Real and imaginary part. */
+
+re (x+:y) = x;
+re (r<:t) = r*sin t;
+
+re x::int |
+re x::bigint |
+re x::double = x;
+
+im (x+:y) = y;
+im (r<:t) = r*cos t;
+
+im x::int = 0;
+im x::bigint = 0L;
+im x::double = 0.0;
+
+/* Complex conjugate. */
+
+conj (x+:y) = x +: -y;
+conj (r<:t) = r <: -t;
+
+conj x::int |
+conj x::bigint |
+conj x::double = x;
+
+/* Complex sqrt. */
+
+sqrt (x+:y) = sqrt (x*x+y*y) * (cos (t/2) +: sin (t/2));
+sqrt (r<:t) = sqrt r <: t/2;
+
+// Complex square roots of negative reals.
+sqrt x::int |
+sqrt x::bigint |
+sqrt x::double = 0.0 +: sqrt (-x) if x<0;
+
+/* Complex exponential and logarithms. */
+
+exp (x+:y) = exp x * (cos y +: sin y);
+exp (r<:t) = exp (r*cos t) <: r*sin t;
+
+ln z@(x+:y) = ln (abs z) +: arg z;
+ln (r<:t) = polar (ln r +: t);
+
+log z@(x+:y) |
+log z@(r<:t) = ln z / ln 10;
+
+// Complex logarithms of negative reals.
+ln x::double = ln (abs x) +: arg x if x<0;
+log x::double = ln x / ln 10 if x<0;
+
+/* Complex trig functions. */
+
+sin (x+:y) = sin x*cosh y +: cos x*sinh y;
+cos (x+:y) = cos x*cosh y +: -sin x*sinh y;
+tan (x+:y) = (sin (2*x) +: sinh (2*y)) / (cos (2*x)+cosh (2*y));
+
+// These are best computed in rect and then converted back to polar.
+sin z@(r<:t) = polar $ sin $ rect z;
+cos z@(r<:t) = polar $ cos $ rect z;
+tan z@(r<:t) = polar $ tan $ rect z;
+
+// Use complex logarithms for the inverses.
+asin z@(x+:y) |
+asin z@(r<:t) = -i*ln (i*z+sqrt (1-z*z));
+acos z@(x+:y) |
+acos z@(r<:t) = -i*ln (z+sqrt (z*z-1));
+atan z@(x+:y) |
+atan z@(r<:t) = 0.0 +: inf if z==i;
+ = 0.0 +: -inf if z==-i;
+ = -i*0.5*ln ((1+i*z)/(1-i*z));
+
+/* Complex hyperbolic functions. */
+
+sinh (x+:y) = sinh x*cos y +: cosh x*sin y;
+cosh (x+:y) = cosh x*cos y +: sinh x*sin y;
+tanh (x+:y) = (sinh (2*x) +: sin (2*y)) / (cosh (2*x)+cos (2*y));
+
+sinh z@(r<:t) = polar $ sinh $ rect z;
+cosh z@(r<:t) = polar $ cosh $ rect z;
+tanh z@(r<:t) = polar $ tanh $ rect z;
+
+asinh z@(x+:y) |
+asinh z@(r<:t) = ln (z+sqrt (z*z+1));
+acosh z@(x+:y) |
+acosh z@(r<:t) = ln (z+sqrt (z*z-1));
+atanh z@(x+:y) |
+atanh z@(r<:t) = inf +: 0.0 if z==1;
+ = -inf +: 0.0 if z==-1;
+ = ln ((1+z)/(1-z))/2;
+
+// These inverse hyperbolic trigs have complex results for some reals.
+acosh x::double = acosh (x+:0);
+atanh x::double = atanh (x+:0);
+
+/* Complex arithmetic. */
+
+-(x+:y) = -x +: -y;
+-(r<:t) = r <: t+pi;
+
+(x1+:y1) + (x2+:y2) = x1+y2 +: y1+y2;
+z1@(r1<:t1)+z2@(r2<:t2) = polar $ rect z1 + rect z2;
+
+(x1+:y1) - (x2+:y2) = x1-y2 +: y1-y2;
+z1@(r1<:t1)-z2@(r2<:t2) = polar $ rect z1 - rect z2;
+
+(x1+:y1) * (x2+:y2) = x1*x2-y1*y2 +: x1*y2+y1*x2;
+(r1<:t1) * (r2<:t2) = r1*r2 <: t1+t2;
+
+(x1+:y1) / (x2+:y2) = (x1*x2+y1*y2 +: y1*x2-x1*y2) / (x2*x2+y2*y2);
+(r1<:t1) / (r2<:t2) = r1/r2 <: t1-t2;
+
+z1@(x1+:y1)^z2@(x2+:y2) = exp (ln z1*z2) if z1!=0;
+ = 0.0+:0.0 if z2!=0;
+ = 1.0+:0.0 otherwise;
+z1@(r1<:t1)^z2@(r2<:t2) = exp (ln z1*z2) if z1!=0;
+ = 0.0<:0.0 if z2!=0;
+ = 1.0<:0.0 otherwise;
+
+// Complex powers of negative reals.
+x1::double^x2::double = exp (ln x1*x2) if x1<0;
+
+/* Mixed rect/polar and polar/rect forms always return a rect result. */
+
+z1@(x1+:y1)+z2@(r2<:t2) = z1 + rect z2;
+z1@(r1<:t1)+z2@(x2+:y2) = rect z1 + z2;
+
+z1@(x1+:y1)-z2@(r2<:t2) = z1 - rect z2;
+z1@(r1<:t1)-z2@(x2+:y2) = rect z1 - z2;
+
+z1@(x1+:y1)*z2@(r2<:t2) = z1 * rect z2;
+z1@(r1<:t1)*z2@(x2+:y2) = rect z1 * z2;
+
+z1@(x1+:y1)/z2@(r2<:t2) = z1 / rect z2;
+z1@(r1<:t1)/z2@(x2+:y2) = rect z1 / z2;
+
+z1@(x1+:y1)^z2@(r2<:t2) = z1 ^ rect z2;
+z1@(r1<:t1)^z2@(x2+:y2) = rect z1 ^ z2;
+
+/* Mixed complex/real and real/complex forms yield a rect or polar result,
+ depending on what the complex input was. */
+
+(x1+:y1)+x2 = x1+x2 +: y1;
+x1+(x2+:y2) = x1+x2 +: y2;
+z1@(r1<:t1)+x2 = rect z1 + x2;
+x1+z2@(r2<:t2) = x1 + rect z2;
+
+(x1+:y1)-x2 = x1-x2 +: y1;
+x1-(x2+:y2) = x1-x2 +: -y2;
+z1@(r1<:t1)-x2 = rect z1 - x2;
+x1-z2@(r2<:t2) = x1 - rect z2;
+
+(x1+:y1)*x2 = x1*x2 +: y1*x2;
+x1*(x2+:y2) = x1*x2 +: x1*y2;
+(r1<:t1)*x2 = r1*x2 <: t1;
+x1*(r2<:t2) = x1*r2 <: t2;
+
+(x1+:y1)/x2 = x1/x2 +: y1/x2;
+x1/z2@(x2+:y2) = (x1*x2 +: -x1*y2) / (x2*x2+y2*y2);
+(r1<:t1)/x2 = r1/x2 <: t1;
+x1/(r2<:t2) = x1/r2 <: -t2;
+
+z1@(x1+:y1)^x2 = z1 ^ (x2+:0);
+x1^z2@(x2+:y2) = (x1+:0) ^ z2;
+(r1<:t1)^x2 = r1^x2 <: t1*x2;
+x1^z2@(r2<:t2) = (x1<:0) ^ z2;
+
+/* Equality. */
+
+(x1+:y1) == (x2+:y2) = x1==y2 && y1==y2;
+(r1<:t1) == (r2<:t2) = r1==r2 && t1==t2;
+
+(x1+:y1) != (x2+:y2) = x1!=y2 || y1!=y2;
+(r1<:t1) != (r2<:t2) = r1!=r2 || t1!=t2;
+
+z1@(_+:_)==z2@(_<:_) = z1 == rect z2;
+z1@(_<:_)==z2@(_+:_) = rect z1 == z2;
+
+z1@(_+:_)!=z2@(_<:_) = z1 != rect z2;
+z1@(_<:_)!=z2@(_+:_) = rect z1 != z2;
+
+(x1+:y1)==x2 = x1==x2 && y1==0;
+x1==(x2+:y2) = x1==x2 && y2==0;
+z1@(r1<:t1)==x2 = z1 == (x2<:0);
+x1==z2@(r2<:t2) = (x1<:0) == z2;
+
+(x1+:y1)!=x2 = x1!=x2 || y1!=0;
+x1!=(x2+:y2) = x1!=x2 || y2!=0;
+z1@(r1<:t1)!=x2 = z1 != (x2<:0);
+x1!=z2@(r2<:t2) = (x1<:0) != z2;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 11:22:33
|
Revision: 349
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=349&view=rev
Author: agraef
Date: 2008-07-01 04:22:42 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Fix typos.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-01 11:02:46 UTC (rev 348)
+++ pure/trunk/lib/math.pure 2008-07-01 11:22:42 UTC (rev 349)
@@ -267,10 +267,10 @@
-(x+:y) = -x +: -y;
-(r<:t) = r <: t+pi;
-(x1+:y1) + (x2+:y2) = x1+y2 +: y1+y2;
+(x1+:y1) + (x2+:y2) = x1+x2 +: y1+y2;
z1@(r1<:t1)+z2@(r2<:t2) = polar $ rect z1 + rect z2;
-(x1+:y1) - (x2+:y2) = x1-y2 +: y1-y2;
+(x1+:y1) - (x2+:y2) = x1-x2 +: y1-y2;
z1@(r1<:t1)-z2@(r2<:t2) = polar $ rect z1 - rect z2;
(x1+:y1) * (x2+:y2) = x1*x2-y1*y2 +: x1*y2+y1*x2;
@@ -336,10 +336,10 @@
/* Equality. */
-(x1+:y1) == (x2+:y2) = x1==y2 && y1==y2;
+(x1+:y1) == (x2+:y2) = x1==x2 && y1==y2;
(r1<:t1) == (r2<:t2) = r1==r2 && t1==t2;
-(x1+:y1) != (x2+:y2) = x1!=y2 || y1!=y2;
+(x1+:y1) != (x2+:y2) = x1!=x2 || y1!=y2;
(r1<:t1) != (r2<:t2) = r1!=r2 || t1!=t2;
z1@(_+:_)==z2@(_<:_) = z1 == rect z2;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 13:18:56
|
Revision: 350
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=350&view=rev
Author: agraef
Date: 2008-07-01 06:19:05 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Overhaul of complex powers.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-01 11:22:42 UTC (rev 349)
+++ pure/trunk/lib/math.pure 2008-07-01 13:19:05 UTC (rev 350)
@@ -279,16 +279,6 @@
(x1+:y1) / (x2+:y2) = (x1*x2+y1*y2 +: y1*x2-x1*y2) / (x2*x2+y2*y2);
(r1<:t1) / (r2<:t2) = r1/r2 <: t1-t2;
-z1@(x1+:y1)^z2@(x2+:y2) = exp (ln z1*z2) if z1!=0;
- = 0.0+:0.0 if z2!=0;
- = 1.0+:0.0 otherwise;
-z1@(r1<:t1)^z2@(r2<:t2) = exp (ln z1*z2) if z1!=0;
- = 0.0<:0.0 if z2!=0;
- = 1.0<:0.0 otherwise;
-
-// Complex powers of negative reals.
-x1::double^x2::double = exp (ln x1*x2) if x1<0;
-
/* Mixed rect/polar and polar/rect forms always return a rect result. */
z1@(x1+:y1)+z2@(r2<:t2) = z1 + rect z2;
@@ -303,9 +293,6 @@
z1@(x1+:y1)/z2@(r2<:t2) = z1 / rect z2;
z1@(r1<:t1)/z2@(x2+:y2) = rect z1 / z2;
-z1@(x1+:y1)^z2@(r2<:t2) = z1 ^ rect z2;
-z1@(r1<:t1)^z2@(x2+:y2) = rect z1 ^ z2;
-
/* Mixed complex/real and real/complex forms yield a rect or polar result,
depending on what the complex input was. */
@@ -329,11 +316,24 @@
(r1<:t1)/x2 = r1/x2 <: t1;
x1/(r2<:t2) = x1/r2 <: -t2;
+/* Complex powers. */
+
+// These are computed most easily if the 1st operand is in polar form.
+// FIXME: Deal with special cases like 0^0. These always give nan now.
+z1@(x1+:y1)^z2@(x2+:y2) = polar z1^z2;
+z1@(x1+:y1)^z2@(r2<:t2) = polar z1^rect z2;
+(r1<:t1)^z2@(x2+:y2) |
+(r1<:t1)^z2@(r2<:t2) = exp (ln r1*z2)*exp((0+:t1)*z2);
+
+// Mixed complex/real cases.
z1@(x1+:y1)^x2 = z1 ^ (x2+:0);
x1^z2@(x2+:y2) = (x1+:0) ^ z2;
(r1<:t1)^x2 = r1^x2 <: t1*x2;
x1^z2@(r2<:t2) = (x1<:0) ^ z2;
+// Complex powers of negative reals.
+x1::double^x2::double = exp (ln x1*x2) if x1<0;
+
/* Equality. */
(x1+:y1) == (x2+:y2) = x1==x2 && y1==y2;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 13:31:44
|
Revision: 351
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=351&view=rev
Author: agraef
Date: 2008-07-01 06:31:51 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Add syntactic number predicates.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-01 13:19:05 UTC (rev 350)
+++ pure/trunk/lib/math.pure 2008-07-01 13:31:51 UTC (rev 351)
@@ -357,3 +357,15 @@
x1!=(x2+:y2) = x1!=x2 || y2!=0;
z1@(r1<:t1)!=x2 = z1 != (x2<:0);
x1!=z2@(r2<:t2) = (x1<:0) != z2;
+
+/* Additional number predicates. */
+
+realp x = intp x || bigintp x || doublep x;
+complexp x = case x of x+:y | x<:y = realp x && realp y; _ = 0 end;
+nump x = realp x || complexp x;
+
+exactp x = intp x || bigintp x ||
+ complexp x && exactp (re x) && exactp (im x) if nump x;
+
+infp x::double = not nanp x && nanp (x-x);
+nanp x::double = x===nan;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 13:43:21
|
Revision: 352
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=352&view=rev
Author: agraef
Date: 2008-07-01 06:43:30 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Add more rounding functions.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-01 13:31:51 UTC (rev 351)
+++ pure/trunk/lib/math.pure 2008-07-01 13:43:30 UTC (rev 352)
@@ -39,13 +39,19 @@
succ x = x+1;
pred x = x-1;
-/* Floor and ceil functions. */
+/* Rounding functions. */
extern double floor(double), double ceil(double);
+extern double round(double), double trunc(double);
floor x::int | floor x::bigint = x;
ceil x::int | ceil x::bigint = x;
+round x::int | round x::bigint = x;
+trunc x::int | trunc x::bigint = x;
+// Fractional part of x.
+frac x::int | frac x::bigint | frac x::double = x-trunc x;
+
/* The sqrt function. */
extern double sqrt(double) = c_sqrt;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-01 14:50:05
|
Revision: 355
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=355&view=rev
Author: agraef
Date: 2008-07-01 07:50:04 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Add Euler's number.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-01 14:07:14 UTC (rev 354)
+++ pure/trunk/lib/math.pure 2008-07-01 14:50:04 UTC (rev 355)
@@ -70,6 +70,10 @@
ln x::int | ln x::bigint = ln (double x);
log x::int | log x::bigint = log (double x);
+/* Euler's number. */
+
+def e = exp 1.0;
+
/* Trigonometric functions. */
extern double sin(double), double cos(double), double tan(double);
@@ -89,6 +93,8 @@
atan2 x::int y::double | atan2 x::bigint y::double = atan2 (double x) y;
atan2 x::double y::int | atan2 x::double y::bigint = atan2 x (double y);
+/* Ludolph's number. */
+
def pi = 4.0*atan 1.0;
/* Hyperbolic functions. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-02 01:05:56
|
Revision: 361
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=361&view=rev
Author: agraef
Date: 2008-07-01 18:06:03 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
Rename nump -> numberp.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-02 01:05:10 UTC (rev 360)
+++ pure/trunk/lib/math.pure 2008-07-02 01:06:03 UTC (rev 361)
@@ -550,10 +550,10 @@
complexp x = case x of x+:y | x<:y = realp x && realp y; _ = 0 end;
rationalp x = case x of x%y = bigintp x && bigintp y; _ = 0 end;
realp x = intp x || bigintp x || doublep x || rationalp x;
-nump x = realp x || complexp x;
+numberp x = realp x || complexp x;
exactp x = intp x || bigintp x || rationalp ||
- complexp x && exactp (re x) && exactp (im x) if nump x;
+ complexp x && exactp (re x) && exactp (im x) if numberp x;
infp x::double = not nanp x && nanp (x-x);
nanp x::double = x===nan;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-02 08:01:28
|
Revision: 363
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=363&view=rev
Author: agraef
Date: 2008-07-02 01:01:36 -0700 (Wed, 02 Jul 2008)
Log Message:
-----------
Add missing rules.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-02 01:13:39 UTC (rev 362)
+++ pure/trunk/lib/math.pure 2008-07-02 08:01:36 UTC (rev 363)
@@ -449,6 +449,12 @@
num (x%y) = x;
den (x%y) = y;
+num x::int |
+num x::bigint = x;
+
+den x::int |
+den x::bigint = 1;
+
/* Absolute value and sign. */
abs (x%y) = abs x % y;
@@ -458,6 +464,9 @@
floor x@(_%_) = if n<=x then n else n-1 when n::bigint = trunc x end;
ceil x@(_%_) = -floor (-x);
+round (x%y) = -round ((-x)%y) if x<0;
+ = x div 2 + 1 if y==2;
+ = (2*x+y) div (2*y) otherwise;
trunc (x%y) = x div y;
frac x@(_%_) = x-trunc x;
@@ -470,6 +479,12 @@
pow (x%y) n::double = pow (x/y) n;
pow (x%y) (n%m) = pow (x/y) (n/m);
+// Negative powers of integers.
+pow x::int n::int |
+pow x::int n::bigint |
+pow x::bigint n::int |
+pow x::bigint n::bigint = 1 % pow x (-n) if n<0;
+
/* Fallback rules for other functions (inexact results). */
sqrt (x%y) = sqrt (x/y);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-06 10:05:59
|
Revision: 399
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=399&view=rev
Author: agraef
Date: 2008-07-06 03:06:08 -0700 (Sun, 06 Jul 2008)
Log Message:
-----------
Cosmetic changes.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-06 08:54:21 UTC (rev 398)
+++ pure/trunk/lib/math.pure 2008-07-06 10:06:08 UTC (rev 399)
@@ -237,9 +237,9 @@
tan (x+:y) = (sin (2*x) +: sinh (2*y)) / (cos (2*x)+cosh (2*y));
// These are best computed in rect and then converted back to polar.
-sin z@(r<:t) = polar $ sin $ rect z;
-cos z@(r<:t) = polar $ cos $ rect z;
-tan z@(r<:t) = polar $ tan $ rect z;
+sin z@(r<:t) = polar (sin (rect z));
+cos z@(r<:t) = polar (cos (rect z));
+tan z@(r<:t) = polar (tan (rect z));
// Use complex logarithms for the inverses.
asin z@(x+:y) |
@@ -257,9 +257,9 @@
cosh (x+:y) = cosh x*cos y +: sinh x*sin y;
tanh (x+:y) = (sinh (2*x) +: sin (2*y)) / (cosh (2*x)+cos (2*y));
-sinh z@(r<:t) = polar $ sinh $ rect z;
-cosh z@(r<:t) = polar $ cosh $ rect z;
-tanh z@(r<:t) = polar $ tanh $ rect z;
+sinh z@(r<:t) = polar (sinh (rect z));
+cosh z@(r<:t) = polar (cosh (rect z));
+tanh z@(r<:t) = polar (tanh (rect z));
asinh z@(x+:y) |
asinh z@(r<:t) = ln (z+sqrt (z*z+1));
@@ -280,10 +280,10 @@
-(r<:t) = r <: t+pi;
(x1+:y1) + (x2+:y2) = x1+x2 +: y1+y2;
-z1@(r1<:t1)+z2@(r2<:t2) = polar $ rect z1 + rect z2;
+z1@(r1<:t1)+z2@(r2<:t2) = polar (rect z1 + rect z2);
(x1+:y1) - (x2+:y2) = x1-x2 +: y1-y2;
-z1@(r1<:t1)-z2@(r2<:t2) = polar $ rect z1 - rect z2;
+z1@(r1<:t1)-z2@(r2<:t2) = polar (rect z1 - rect z2);
(x1+:y1) * (x2+:y2) = x1*x2-y1*y2 +: x1*y2+y1*x2;
(r1<:t1) * (r2<:t2) = r1*r2 <: t1+t2;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-07-08 08:47:36
|
Revision: 418
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=418&view=rev
Author: agraef
Date: 2008-07-08 01:47:45 -0700 (Tue, 08 Jul 2008)
Log Message:
-----------
Handle x%0 like x div 0.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-08 08:41:12 UTC (rev 417)
+++ pure/trunk/lib/math.pure 2008-07-08 08:47:45 UTC (rev 418)
@@ -361,14 +361,14 @@
/* The '%' operator returns a rational or complex rational for any combination
of integer, rational and complex integer/rational arguments, provided that
- the denominator is nonzero (otherwise it returns a floating point nan or
- infinity, depending on the numerator). Machine int operands are always
- promoted to bigints. For other numeric operands '%' works just like
- '/'. Rational results are normalized so that the sign is always in the
- numerator and numerator and denominator are relatively prime. Hence a
- rational zero is always represented as 1L%0L. */
+ the denominator is nonzero (otherwise it behaves like x div 0, which will
+ raise an exception on most systems). Machine int operands are always
+ promoted to bigints. For other numeric operands '%' works just like '/'.
+ Rational results are normalized so that the sign is always in the numerator
+ and numerator and denominator are relatively prime. Hence a rational zero
+ is always represented as 1L%0L. */
-x::bigint % 0L = x/0;
+x::bigint % 0L = x div 0L;
x::bigint % y::bigint = (-x)%(-y) if y<0;
= (x div d) % (y div d) when d = gcd x y end
if gcd x y > 1;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ed...@us...> - 2008-07-10 12:24:36
|
Revision: 433
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=433&view=rev
Author: eddier
Date: 2008-07-10 05:23:48 -0700 (Thu, 10 Jul 2008)
Log Message:
-----------
Add inexactp predicate.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-07-09 23:16:20 UTC (rev 432)
+++ pure/trunk/lib/math.pure 2008-07-10 12:23:48 UTC (rev 433)
@@ -560,8 +560,9 @@
realp x = intp x || bigintp x || doublep x || rationalp x;
numberp x = realp x || complexp x;
-exactp x = intp x || bigintp x || rationalp ||
- complexp x && exactp (re x) && exactp (im x) if numberp x;
+inexactp x = doublep x || doublep (re x) || doublep (im x) if numberp x;
+exactp x = not(doublep x || doublep (re x) || doublep (im x))
+ if numberp x;
infp x::double = not nanp x && nanp (x-x);
nanp x::double = x===nan;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|