Thread: [pure-lang-svn] SF.net SVN: pure-lang:[487] pure/trunk/lib/math.pure
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-08-14 06:39:28
|
Revision: 487
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=487&view=rev
Author: agraef
Date: 2008-08-14 06:39:38 +0000 (Thu, 14 Aug 2008)
Log Message:
-----------
Comment change.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-13 13:51:40 UTC (rev 486)
+++ pure/trunk/lib/math.pure 2008-08-14 06:39:38 UTC (rev 487)
@@ -154,7 +154,7 @@
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. */
+ exp (x+:y) in polar form, use exp x <: y. */
cis t = rect (1<:t);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-14 06:56:14
|
Revision: 488
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=488&view=rev
Author: agraef
Date: 2008-08-14 06:56:25 +0000 (Thu, 14 Aug 2008)
Log Message:
-----------
Comment change.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-14 06:39:38 UTC (rev 487)
+++ pure/trunk/lib/math.pure 2008-08-14 06:56:25 UTC (rev 488)
@@ -368,11 +368,11 @@
/* 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 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. */
+ raise an exception). 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 0L%1L. */
x::bigint % 0L = x div 0L;
x::bigint % y::bigint = (-x)%(-y) if y<0;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-14 07:30:36
|
Revision: 489
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=489&view=rev
Author: agraef
Date: 2008-08-14 07:30:45 +0000 (Thu, 14 Aug 2008)
Log Message:
-----------
Add missing complex functions on rationals.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-14 06:56:25 UTC (rev 488)
+++ pure/trunk/lib/math.pure 2008-08-14 07:30:45 UTC (rev 489)
@@ -453,6 +453,13 @@
abs (x%y) = abs x % y;
sgn (x%y) = sgn x;
+/* Complex functions. */
+
+arg (x%y) = atan2 0 (x/y);
+re x@(_%_) = x;
+im (_%_) = 0L%1L;
+conj x@(_%_) = x;
+
/* Rounding functions. These return exact results here. */
floor x@(_%_) = if n<=x then n else n-1 when n::bigint = trunc x end;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-14 12:22:44
|
Revision: 492
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=492&view=rev
Author: agraef
Date: 2008-08-14 12:22:54 +0000 (Thu, 14 Aug 2008)
Log Message:
-----------
Overhaul of number predicates.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-14 07:59:36 UTC (rev 491)
+++ pure/trunk/lib/math.pure 2008-08-14 12:22:54 UTC (rev 492)
@@ -563,13 +563,33 @@
/* Additional number predicates. */
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;
+realp x = case x of _::int | _::bigint | _::double |
+ _::bigint%_::bigint = 1; _ = 0 end;
+rationalp x = case x of _::bigint%_::bigint = 1; _ = 0 end;
numberp x = realp x || complexp 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;
+/* Semantic number predicates. In difference to the syntactic predicates in
+ primitives.pure and above, these check whether the given value can be
+ represented as an object of the given target type (up to rounding
+ errors). Note that if x is of syntactic type X, then it is also of semantic
+ type X. Moreover, intvalp x => bigintvalp x => ratvalp x => realvalp x =>
+ compvalp x <=> numberp x. */
-infp x::double = not nanp x && nanp (x-x);
-nanp x::double = x===nan;
+compvalp x = numberp x;
+realvalp x = compvalp x && im x==0;
+ratvalp x = realvalp x && (y-y!==nan when y = re x end);
+bigintvalp x = ratvalp x && den x==1;
+intvalp x = bigintvalp && int x==x;
+
+/* Check whether a number is exact (i.e., doesn't contain any double
+ components. */
+
+exactp x = numberp x &&
+ not (doublep x || doublep (re x) || doublep (im x));
+inexactp x = numberp x &&
+ (doublep x || doublep (re x) || doublep (im x));
+
+/* Check for inf and nan values. */
+
+infp x = case x of x::double = x!==nan && x-x===nan; _ = 0 end;
+nanp x = case x of x::double = x===nan; _ = 0 end;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-14 12:30:06
|
Revision: 493
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=493&view=rev
Author: agraef
Date: 2008-08-14 12:30:15 +0000 (Thu, 14 Aug 2008)
Log Message:
-----------
Bugfixes in num/den and number predicates.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-14 12:22:54 UTC (rev 492)
+++ pure/trunk/lib/math.pure 2008-08-14 12:30:15 UTC (rev 493)
@@ -443,10 +443,12 @@
den (x%y) = y;
num x::int |
-num x::bigint = x;
+num x::bigint = bigint x;
+num x::double = if frac x==0.0 then bigint x else num (rational x);
den x::int |
-den x::bigint = 1;
+den x::bigint = 1L;
+den x::double = if frac x==0.0 then 1L else den (rational x);
/* Absolute value and sign. */
@@ -577,9 +579,9 @@
compvalp x = numberp x;
realvalp x = compvalp x && im x==0;
-ratvalp x = realvalp x && (y-y!==nan when y = re x end);
-bigintvalp x = ratvalp x && den x==1;
-intvalp x = bigintvalp && int x==x;
+ratvalp x = realvalp x && re (x-x)!==nan;
+bigintvalp x = ratvalp x && den (re x)==1;
+intvalp x = bigintvalp x && int (re x)==x;
/* Check whether a number is exact (i.e., doesn't contain any double
components. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-18 17:30:50
|
Revision: 532
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=532&view=rev
Author: agraef
Date: 2008-08-18 17:30:58 +0000 (Mon, 18 Aug 2008)
Log Message:
-----------
Fix glitch in complex sqrt, reported by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-18 11:47:30 UTC (rev 531)
+++ pure/trunk/lib/math.pure 2008-08-18 17:30:58 UTC (rev 532)
@@ -32,7 +32,7 @@
extern double sqrt(double) = c_sqrt;
-sqrt x::double = c_sqrt x if x>=0;
+sqrt x::double = c_sqrt x if x>=0;
sqrt x::int | sqrt x::bigint = sqrt (double x);
/* Exponential function and logarithms. */
@@ -185,7 +185,8 @@
/* Complex sqrt. */
-sqrt (x+:y) = sqrt (x*x+y*y) * (cos (t/2) +: sin (t/2));
+sqrt (x+:y) = sqrt r * (cos t +: sin t)
+ when r = sqrt (x*x+y*y); t = atan2 y x/2 end;
sqrt (r<:t) = sqrt r <: t/2;
// Complex square roots of negative reals.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-18 17:43:49
|
Revision: 533
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=533&view=rev
Author: agraef
Date: 2008-08-18 17:43:57 +0000 (Mon, 18 Aug 2008)
Log Message:
-----------
Add missing rules for pow function, reported by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-18 17:30:58 UTC (rev 532)
+++ pure/trunk/lib/math.pure 2008-08-18 17:43:57 UTC (rev 533)
@@ -468,6 +468,9 @@
= 1L%1L otherwise;
pow (x%y) n::double = pow (x/y) n;
pow (x%y) (n%m) = pow (x/y) (n/m);
+pow x::int (n%m) |
+pow x::bigint (n%m) |
+pow x::double (n%m) = pow x (n/m);
// Negative powers of integers.
pow x::int n::int |
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-18 23:38:28
|
Revision: 535
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=535&view=rev
Author: agraef
Date: 2008-08-18 23:38:38 +0000 (Mon, 18 Aug 2008)
Log Message:
-----------
Better complex sqrt definition, suggested by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-18 23:01:53 UTC (rev 534)
+++ pure/trunk/lib/math.pure 2008-08-18 23:38:38 UTC (rev 535)
@@ -185,8 +185,8 @@
/* Complex sqrt. */
-sqrt (x+:y) = sqrt r * (cos t +: sin t)
- when r = sqrt (x*x+y*y); t = atan2 y x/2 end;
+sqrt (x+:y) = sqrt ((r+x)/2) +: sqrt ((r-x)/2)
+ when r = sqrt (x*x+y*y) end;
sqrt (r<:t) = sqrt r <: t/2;
// Complex square roots of negative reals.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-21 11:49:25
|
Revision: 556
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=556&view=rev
Author: agraef
Date: 2008-08-21 11:49:34 +0000 (Thu, 21 Aug 2008)
Log Message:
-----------
Make polar and rect the identity on the respective representations. Suggested by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-21 11:31:10 UTC (rev 555)
+++ pure/trunk/lib/math.pure 2008-08-21 11:49:34 UTC (rev 556)
@@ -132,6 +132,9 @@
polar (x+:y) = sqrt (x*x+y*y) <: atan2 y x;
rect (r<:t) = r*cos t +: r*sin t;
+polar z@(_<:_) = z;
+rect z@(_+:_) = z;
+
/* For convenience, make these work with real values, too. */
polar x::int | polar x::bigint = x<:0;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-21 13:28:02
|
Revision: 557
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=557&view=rev
Author: agraef
Date: 2008-08-21 13:28:11 +0000 (Thu, 21 Aug 2008)
Log Message:
-----------
Properly deal with nan arguments in sqrt, log and hyperbolic functions. Reported by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-21 11:49:34 UTC (rev 556)
+++ pure/trunk/lib/math.pure 2008-08-21 13:28:11 UTC (rev 557)
@@ -32,15 +32,15 @@
extern double sqrt(double) = c_sqrt;
-sqrt x::double = c_sqrt x if x>=0;
+sqrt x::double = c_sqrt x if x>=0 || nanp x;
sqrt x::int | sqrt x::bigint = sqrt (double x);
/* 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;
+ln x::double = c_log x if x>=0.0 || nanp x;
+log x::double = c_log x/c_log 10.0 if x>=0.0 || nanp x;
exp x::int | exp x::bigint = exp (double x);
ln x::int | ln x::bigint = ln (double x);
@@ -79,8 +79,8 @@
extern double __asinh(double), double __acosh(double), double __atanh(double);
asinh x::double = __asinh x;
-acosh x::double = __acosh x if x>=1.0;
-atanh x::double = __atanh x if abs x<=1.0;
+acosh x::double = __acosh x if x>=1.0 || nanp x;
+atanh x::double = __atanh x if abs x<=1.0 || nanp x;
sinh x::int | sinh x::bigint = sinh (double x);
cosh x::int | cosh x::bigint = cosh (double x);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-21 13:51:28
|
Revision: 558
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=558&view=rev
Author: agraef
Date: 2008-08-21 13:51:39 +0000 (Thu, 21 Aug 2008)
Log Message:
-----------
cis should only be used with real arguments. Reported by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-21 13:28:11 UTC (rev 557)
+++ pure/trunk/lib/math.pure 2008-08-21 13:51:39 UTC (rev 558)
@@ -146,7 +146,7 @@
/* Create complex values on the unit circle. Note: To quickly compute
exp (x+:y) in polar form, use exp x <: y. */
-cis t = rect (1<:t);
+cis t::int | cis t::bigint | cis t::double = 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. */
@@ -428,6 +428,8 @@
r<:t@(_%_) = r <: atan2 (sin t) (cos t) if t<-pi || t>pi;
= r <: pi if t==-pi;
+cis (x%y) = rect (1<:x/y);
+
/* Numerator and denominator. */
num (x%y) = x;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-23 10:27:25
|
Revision: 576
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=576&view=rev
Author: agraef
Date: 2008-08-23 10:27:31 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Fix broken complex sqrt, minor cosmetic changes in complex trig and hyperbolic functions.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-22 20:24:48 UTC (rev 575)
+++ pure/trunk/lib/math.pure 2008-08-23 10:27:31 UTC (rev 576)
@@ -183,8 +183,8 @@
/* Complex sqrt. */
-sqrt (x+:y) = sqrt ((r+x)/2) +: sqrt ((r-x)/2)
- when r = sqrt (x*x+y*y) end;
+sqrt (x+:y) = sqrt r*(cos t +: sin t)
+ when r = sqrt (x*x+y*y); t = atan2 y x/2 end;
sqrt (r<:t) = sqrt r <: t/2;
/* Complex exponential and logarithms. */
@@ -215,9 +215,7 @@
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));
+atan z@(r<:t) = (ln (1+i*z)-ln (1-i*z))/(2*i);
/* Complex hyperbolic functions. */
@@ -233,10 +231,11 @@
asinh z@(r<:t) = ln (z+sqrt (z*z+1));
acosh z@(x+:y) |
acosh z@(r<:t) = ln (z+sqrt (z*z-1));
+// Alternative definition (Kahan).
+// acosh z@(x+:y) |
+// acosh z@(r<:t) = 2*ln (sqrt ((z+1)/2)+sqrt ((z-1)/2));
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;
+atanh z@(r<:t) = (ln (1+z)-ln (1-z))/2;
/* Complex arithmetic. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-23 10:45:17
|
Revision: 577
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=577&view=rev
Author: agraef
Date: 2008-08-23 10:45:28 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Fix up definition of complex powers.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-23 10:27:31 UTC (rev 576)
+++ pure/trunk/lib/math.pure 2008-08-23 10:45:28 UTC (rev 577)
@@ -293,19 +293,11 @@
/* 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);
+z1@(_+:_)^z2 |
+z1^z2@(_+:_) |
+z1@(_<:_)^z2 |
+z1^z2@(_<:_) = exp (ln z1*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;
-
/* 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-08-23 11:09:03
|
Revision: 578
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=578&view=rev
Author: agraef
Date: 2008-08-23 11:09:12 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Bugfix in (^) operation.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-23 10:45:28 UTC (rev 577)
+++ pure/trunk/lib/math.pure 2008-08-23 11:09:12 UTC (rev 578)
@@ -293,10 +293,10 @@
/* Complex powers. */
-z1@(_+:_)^z2 |
-z1^z2@(_+:_) |
-z1@(_<:_)^z2 |
-z1^z2@(_<:_) = exp (ln z1*z2);
+z1@(_+:_)^x2 |
+z1@(_<:_)^x2 = exp (ln z1*x2);
+x1^z2@(_+:_) |
+x1^z2@(_<:_) = exp (ln (complex x1)*z2);
/* Equality. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-23 20:50:12
|
Revision: 583
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=583&view=rev
Author: agraef
Date: 2008-08-23 20:50:22 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Add type guards to rational operations.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-23 12:56:48 UTC (rev 582)
+++ pure/trunk/lib/math.pure 2008-08-23 20:50:22 UTC (rev 583)
@@ -349,8 +349,10 @@
// rational operands
(x1%y1)%(x2%y2) = (x1*y2)%(y1*x2);
-(x1%y1)%x2 = x1%(y1*x2);
-x1%(x2%y2) = (x1*y2)%x2;
+(x1%y1)%x2::int |
+(x1%y1)%x2::bigint = x1%(y1*x2);
+x1::int%(x2%y2) |
+x1::bigint%(x2%y2) = (x1*y2)%x2;
// complex operands (these must both use the same representation, otherwise
// the result won't be exact)
@@ -358,15 +360,19 @@
z1@(_<:_)%z2@(_+:_) = z1/z2;
(x1+:y1)%(x2+:y2) = (x1*x2+y1*y2)%d +: (y1*x2-x1*y2)%d
when d = x2*x2+y2*y2 end;
-(x1+:y1)%x2 = (x1*x2)%d +: (y1*x2)%d when d = x2*x2 end;
-x1%(x2+:y2) = (x1*x2)%d +: (-x1*y2)%d when d = x2*x2+y2*y2 end;
(r1<:t1)%(r2<:t2) = r1%r2 <: t1-t2;
-(r1<:t1)%x2 = r1%x2 <: t1;
-x1%(r2<:t2) = x1%r2 <: -t2;
+// mixed complex/real cases
+(x1+:y1)%x2 = (x1*x2)%d +: (y1*x2)%d
+ when d = x2*x2 end if realp x2;
+x1%(x2+:y2) = (x1*x2)%d +: (-x1*y2)%d
+ when d = x2*x2+y2*y2 end if realp x1;
+(r1<:t1)%x2 = r1%x2 <: t1 if realp x2;
+x1%(r2<:t2) = x1%r2 <: -t2 if realp x1;
+
// fall back to ordinary inexact division in all other cases
-x::double%y |
-x%y::double = x/y;
+x::double%y = x/y if numberp y;
+x%y::double = x/y if numberp x;
/* Conversions. */
@@ -484,24 +490,37 @@
(x1%y1)-(x2%y2) = (x1*y2-x2*y1) % (y1*y2);
(x1%y1)*(x2%y2) = (x1*x2) % (y1*y2);
-(x1%y1)+x2 = (x1+x2*y1) % y1;
-(x1%y1)-x2 = (x1-x2*y1) % y1;
-(x1%y1)*x2 = (x1*x2) % y1;
+(x1%y1)+x2::int |
+(x1%y1)+x2::bigint = (x1+x2*y1) % y1;
+(x1%y1)-x2::int |
+(x1%y1)-x2::bigint = (x1-x2*y1) % y1;
+(x1%y1)*x2::int |
+(x1%y1)*x2::bigint = (x1*x2) % y1;
-x1+(x2%y2) = (x1*y2+x2) % y2;
-x1-(x2%y2) = (x1*y2-x2) % y2;
-x1*(x2%y2) = (x1*x2) % y2;
+x1::int+(x2%y2) |
+x1::bigint+(x2%y2) = (x1*y2+x2) % y2;
+x1::int-(x2%y2) |
+x1::bigint-(x2%y2) = (x1*y2-x2) % y2;
+x1::int*(x2%y2) |
+x1::bigint*(x2%y2) = (x1*x2) % y2;
-/* / and ^ yield inexact results. */
+/* Fallback rules. */
-(x1%y1)/(x2%y2) = (x1*y2) / (y1*x2);
+// / and ^ always yield inexact results.
+(x1%y1)/(x2%y2) = (x1/y1) / (x2/y2);
(x1%y1)^(x2%y2) = (x1/y1) ^ (x2/y2);
-(x1%y1)/x2 = x1 / (y1*x2);
-(x1%y1)^x2 = (x1/y1) ^ x2;
+(x1%y1)+x2 = (x1/y1)+x2 if numberp x2;
+(x1%y1)-x2 = (x1/y1)-x2 if numberp x2;
+(x1%y1)*x2 = (x1/y1)*x2 if numberp x2;
+(x1%y1)/x2 = (x1/y1)/x2 if numberp x2;
+(x1%y1)^x2 = (x1/y1)^x2 if numberp x2;
-x1/(x2%y2) = (x1*y2) / x2;
-x1^(x2%y2) = x1 ^ (x2/y2);
+x1+(x2%y2) = x1+(x2/y2) if numberp x1;
+x1-(x2%y2) = x1-(x2/y2) if numberp x1;
+x1*(x2%y2) = x1*(x2/y2) if numberp x1;
+x1/(x2%y2) = x1/(x2/y2) if numberp x1;
+x1^(x2%y2) = x1^(x2/y2) if numberp x1;
/* Comparisons. */
@@ -512,19 +531,19 @@
x1%y1 > x2%y2 = x1*y2 > x2*y1;
x1%y1 >= x2%y2 = x1*y2 >= x2*y1;
-x1%y1 == x2 = x1 == x2*y1;
-x1%y1 != x2 = x1 != x2*y1;
-x1%y1 < x2 = x1 < x2*y1;
-x1%y1 <= x2 = x1 <= x2*y1;
-x1%y1 > x2 = x1 > x2*y1;
-x1%y1 >= x2 = x1 >= x2*y1;
+x1%y1 == x2 = x1 == x2*y1 if numberp x2;
+x1%y1 != x2 = x1 != x2*y1 if numberp x2;
+x1%y1 < x2 = x1 < x2*y1 if realp x2;
+x1%y1 <= x2 = x1 <= x2*y1 if realp x2;
+x1%y1 > x2 = x1 > x2*y1 if realp x2;
+x1%y1 >= x2 = x1 >= x2*y1 if realp x2;
-x1 == x2%y2 = x1*y2 == x2;
-x1 != x2%y2 = x1*y2 != x2;
-x1 < x2%y2 = x1*y2 < x2;
-x1 <= x2%y2 = x1*y2 <= x2;
-x1 > x2%y2 = x1*y2 > x2;
-x1 >= x2%y2 = x1*y2 >= x2;
+x1 == x2%y2 = x1*y2 == x2 if numberp x2;
+x1 != x2%y2 = x1*y2 != x2 if numberp x2;
+x1 < x2%y2 = x1*y2 < x2 if realp x2;
+x1 <= x2%y2 = x1*y2 <= x2 if realp x2;
+x1 > x2%y2 = x1*y2 > x2 if realp x2;
+x1 >= x2%y2 = x1*y2 >= x2 if realp x2;
/* Additional number predicates. */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-23 21:46:02
|
Revision: 585
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=585&view=rev
Author: agraef
Date: 2008-08-23 21:46:12 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Add type guards to complex operations.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-23 20:58:35 UTC (rev 584)
+++ pure/trunk/lib/math.pure 2008-08-23 21:46:12 UTC (rev 585)
@@ -271,32 +271,32 @@
/* 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 if realp x2;
+x1+(x2+:y2) = x1+x2 +: y2 if realp x1;
+z1@(r1<:t1)+x2 = rect z1 + x2 if realp x2;
+x1+z2@(r2<:t2) = x1 + rect z2 if realp x1;
-(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 if realp x2;
+x1-(x2+:y2) = x1-x2 +: -y2 if realp x1;
+z1@(r1<:t1)-x2 = rect z1 - x2 if realp x2;
+x1-z2@(r2<:t2) = x1 - rect z2 if realp x1;
-(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 if realp x2;
+x1*(x2+:y2) = x1*x2 +: x1*y2 if realp x1;
+(r1<:t1)*x2 = r1*x2 <: t1 if realp x2;
+x1*(r2<:t2) = x1*r2 <: t2 if realp x1;
-(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;
+(x1+:y1)/x2 = x1/x2 +: y1/x2 if realp x2;
+x1/z2@(x2+:y2) = (x1*x2 +: -x1*y2) / (x2*x2+y2*y2) if realp x1;
+(r1<:t1)/x2 = r1/x2 <: t1 if realp x2;
+x1/(r2<:t2) = x1/r2 <: -t2 if realp x1;
/* Complex powers. */
z1@(_+:_)^x2 |
-z1@(_<:_)^x2 = exp (ln z1*x2);
-x1^z2@(_+:_) = exp (ln (rect x1)*z2);
-x1^z2@(_<:_) = exp (ln (polar x1)*z2);
+z1@(_<:_)^x2 = exp (ln z1*x2) if numberp x2;
+x1^z2@(_+:_) = exp (ln (rect x1)*z2) if realp x1;
+x1^z2@(_<:_) = exp (ln (polar x1)*z2) if realp x1;
/* Equality. */
@@ -312,15 +312,15 @@
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 if realp x2;
+x1==(x2+:y2) = x1==x2 && y2==0 if realp x1;
+z1@(r1<:t1)==x2 = z1 == (x2<:0) if realp x2;
+x1==z2@(r2<:t2) = (x1<:0) == z2 if realp x1;
-(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 if realp x2;
+x1!=(x2+:y2) = x1!=x2 || y2!=0 if realp x1;
+z1@(r1<:t1)!=x2 = z1 != (x2<:0) if realp x2;
+x1!=z2@(r2<:t2) = (x1<:0) != z2 if realp x1;
/* Rational numbers. These are constructed with the exact division operator
'%' which has the same precedence and fixity as the other division
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-08-23 22:35:56
|
Revision: 590
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=590&view=rev
Author: agraef
Date: 2008-08-23 22:36:04 +0000 (Sat, 23 Aug 2008)
Log Message:
-----------
Add missing type guard to atan2 function.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-08-23 22:24:50 UTC (rev 589)
+++ pure/trunk/lib/math.pure 2008-08-23 22:36:04 UTC (rev 590)
@@ -472,8 +472,8 @@
acos (x%y) = acos (x/y);
atan (x%y) = atan (x/y);
-atan2 (x%y) z = atan2 (x/y) z;
-atan2 x (y%z) = atan2 x (y/z);
+atan2 (x%y) z = atan2 (x/y) z if realp z;
+atan2 x (y%z) = atan2 x (y/z) if realp x;
sinh (x%y) = sinh (x/y);
cosh (x%y) = cosh (x/y);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-09-11 14:40:56
|
Revision: 748
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=748&view=rev
Author: agraef
Date: 2008-09-11 14:41:07 +0000 (Thu, 11 Sep 2008)
Log Message:
-----------
Make mixed complex/real cases of * and / return results consistent with complex/complex cases. Reported by Eddie Rucker.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-09-09 10:12:35 UTC (rev 747)
+++ pure/trunk/lib/math.pure 2008-09-11 14:41:07 UTC (rev 748)
@@ -246,10 +246,14 @@
(x1+:y1) - (x2+:y2) = x1-x2 +: y1-y2;
z1@(r1<:t1)-z2@(r2<:t2) = polar (rect z1 - rect z2);
+// TODO: Review ISO/IEC9899 Annex G recommendations for * and / to fix up
+// nan+:nan results in rectangular representation.
+
(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);
+(x1+:y1) / (x2+:y2) = (x1*x2+y1*y2)/d +: (y1*x2-x1*y2)/d
+ when d = x2*x2+y2*y2 end;
(r1<:t1) / (r2<:t2) = r1/r2 <: t1-t2;
/* Mixed rect/polar and polar/rect forms always return a rect result. */
@@ -279,13 +283,13 @@
z1@(r1<:t1)-x2 = rect z1 - x2 if realp x2;
x1-z2@(r2<:t2) = x1 - rect z2 if realp x1;
-(x1+:y1)*x2 = x1*x2 +: y1*x2 if realp x2;
-x1*(x2+:y2) = x1*x2 +: x1*y2 if realp x1;
+z1@(x1+:y1)*x2 = z1 * rect x2 if realp x2;
+x1*z2@(x2+:y2) = rect x1 * z2 if realp x1;
(r1<:t1)*x2 = r1*x2 <: t1 if realp x2;
x1*(r2<:t2) = x1*r2 <: t2 if realp x1;
-(x1+:y1)/x2 = x1/x2 +: y1/x2 if realp x2;
-x1/z2@(x2+:y2) = (x1*x2 +: -x1*y2) / (x2*x2+y2*y2) if realp x1;
+z1@(x1+:y1)/x2 = z1 / rect x2 if realp x2;
+x1/z2@(x2+:y2) = rect x1 / z2 if realp x1;
(r1<:t1)/x2 = r1/x2 <: t1 if realp x2;
x1/(r2<:t2) = x1/r2 <: -t2 if realp x1;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ag...@us...> - 2008-09-11 14:47:28
|
Revision: 749
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=749&view=rev
Author: agraef
Date: 2008-09-11 14:47:38 +0000 (Thu, 11 Sep 2008)
Log Message:
-----------
Mixed polar/real cases of + and - now return polar results as advertized.
Modified Paths:
--------------
pure/trunk/lib/math.pure
Modified: pure/trunk/lib/math.pure
===================================================================
--- pure/trunk/lib/math.pure 2008-09-11 14:41:07 UTC (rev 748)
+++ pure/trunk/lib/math.pure 2008-09-11 14:47:38 UTC (rev 749)
@@ -275,13 +275,13 @@
(x1+:y1)+x2 = x1+x2 +: y1 if realp x2;
x1+(x2+:y2) = x1+x2 +: y2 if realp x1;
-z1@(r1<:t1)+x2 = rect z1 + x2 if realp x2;
-x1+z2@(r2<:t2) = x1 + rect z2 if realp x1;
+z1@(r1<:t1)+x2 = z1 + polar x2 if realp x2;
+x1+z2@(r2<:t2) = polar x1 + z2 if realp x1;
(x1+:y1)-x2 = x1-x2 +: y1 if realp x2;
x1-(x2+:y2) = x1-x2 +: -y2 if realp x1;
-z1@(r1<:t1)-x2 = rect z1 - x2 if realp x2;
-x1-z2@(r2<:t2) = x1 - rect z2 if realp x1;
+z1@(r1<:t1)-x2 = z1 - polar x2 if realp x2;
+x1-z2@(r2<:t2) = polar x1 - z2 if realp x1;
z1@(x1+:y1)*x2 = z1 * rect x2 if realp x2;
x1*z2@(x2+:y2) = rect x1 * z2 if realp x1;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|