Re: [fricas-devel] [PATCH] get inline optimization for some Integer functions

2018-11-21 Thread Waldek Hebisch
oldk1331 wrote:
> 
> I said before that the following test takes too much time to run:
>  testIntegrate("sqrt(tan(x)^2 + 2*tan(x) + 2)", "x", "alg")
> 
> I did some profiling, and found that 40% time is spent in
> symmetricRemainder:
> 
> Callers
>   Total. Function
>   Count %  Count %  Callees
> 
>   3   0.0   |MODRING;reduce;RMod$;6| [76]
>  13   0.1   |INS-;symmetricRemainder;3S;27| [1]
>3431  39.3   |IDPO;map;M2$;6| [5]
>1104  12.7   3451  39.6   |INS-;symmetricRemainder;3S;27| [1]
>  13   0.1   |INS-;symmetricRemainder;3S;27| [1]
> 160   1.8   |INT;=;2$B;30| [4]
> 149   1.7   |ABELMON-;*;Pi2S;2| [14]
>  67   0.8   SB-VM::ALLOC-TRAMP [15]
> 228   2.6   |ORDSET-;>;2SB;4| [7]
> 173   2.0   |ORDSET-;<=;2SB;6| [20]
>  73   0.8   |INT;+;3$;34| [9]
> 160   1.8   |INT;*;3$;37| [6]
> 180   2.1   |ABELGRP-;*;Nni2S;3| [11]
> 366   4.2   |INT;rem;3$;44| [16]
> 369   4.2   IDENTITY [2]
> 407   4.7   |INT;<;2$B;31| [3]
> 
> 
> We can see that "=$INT, +$INT, *$INT, <$INT" are not inlined, and
> "*$ABELMON, *$ABELGRP, >, <=" are not defined in INT, thus impossible
> to get inlined.
> 
> So I want to add inline optimization for these functions.
> (I also add inline support for "positive?, even?, ^")
> 
> === test case
> )time on
> )lisp (require :sb-sprof)
> 
> f := sqrt(tan(x)^2 + 2*tan(x) + 2)
> g := integrate(f, x)
> 
> f2 := D(g, x)
> 
> )lisp (sb-sprof:start-profiling)
> normalize(f-f2)
> )lisp (sb-sprof:stop-profiling)
> 
> )lisp (sb-sprof:report)
> ===
> 
> Benchmark result:
>  D(g, x)normalize(f-f2)
> before patch40.3 s  87.4 s
> after patch 28.4 s  57.3 s
> 
> So it's 30% faster, saves 40 seconds in testing.

OK, good.  But I see some funny formatting below, which may be
due tabs.  Please make sure there are no tabs when you commit.

> 
> diff --git a/src/algebra/integer.spad b/src/algebra/integer.spad
> index 0e619c2f..35f673b2 100644
> --- a/src/algebra/integer.spad
> +++ b/src/algebra/integer.spad
> @@ -59,7 +59,6 @@
> ZP ==> SparseUnivariatePolynomial %
> ZZP ==> SparseUnivariatePolynomial Integer
> x, y : %
> -  n : NonNegativeInteger
 ^^
Would be indentation error if real.

-- 
  Waldek Hebisch

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To post to this group, send email to fricas-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [fricas-devel] erf is left unevaluated in expression

2018-11-21 Thread oldk1331
On Wed, Nov 21, 2018 at 6:22 PM Slawomir Kolodynski  wrote:
>
> lack of a basic form of referential transparency between variables and 
> literals.
> Namely if you assign a literal to a variable and later in code use a variable,
> the result may be different than if you had used the literal instead. For 
> example:

This is not a problem about "referential transparency", this is about a old bug
related with (numeric) evaluation of "erf", e.g. "eval(erf x, x, 1.0)"
returns "erf(1.0)".

https://groups.google.com/forum/#!msg/fricas-devel/Rz5_1iBadAY/B2g9GVibCwAJ


> And another one using the variable which stores that literal
>
> C1(S:F,r:F,T:F,K:F,s:F):F == BS

This usage is wrong, BS is already defined, with a type of Expression,
so this line will never work.

One way can work:
First, use macro '==>' instead of assignment.
Second, use "exp" instead of "%e", so that they are
defined in DFLOAT:

BS1 ==> 
((S*erf(((2*log((S/K))+T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*exp((-T*r))*erf(((2*log((S/K))-T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*exp((-T*r))+S)/2)
C1(S:F,r:F,T:F,K:F,s:F):F == BS1
C1(10.0,0.05,0.8,11.0,0.05)
   Compiling function C1 with type (DoubleFloat, DoubleFloat,
DoubleFloat, DoubleFloat, DoubleFloat) -> DoubleFloat

   (18)  0.023903294619544546


I think after the bug I mentioned get fixed, using "numeric" will also work.

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To post to this group, send email to fricas-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.


[fricas-devel] [PATCH] get inline optimization for some Integer functions

2018-11-21 Thread oldk1331
I said before that the following test takes too much time to run:
 testIntegrate("sqrt(tan(x)^2 + 2*tan(x) + 2)", "x", "alg")

I did some profiling, and found that 40% time is spent in
symmetricRemainder:

Callers
  Total. Function
  Count %  Count %  Callees

  3   0.0   |MODRING;reduce;RMod$;6| [76]
 13   0.1   |INS-;symmetricRemainder;3S;27| [1]
   3431  39.3   |IDPO;map;M2$;6| [5]
   1104  12.7   3451  39.6   |INS-;symmetricRemainder;3S;27| [1]
 13   0.1   |INS-;symmetricRemainder;3S;27| [1]
160   1.8   |INT;=;2$B;30| [4]
149   1.7   |ABELMON-;*;Pi2S;2| [14]
 67   0.8   SB-VM::ALLOC-TRAMP [15]
228   2.6   |ORDSET-;>;2SB;4| [7]
173   2.0   |ORDSET-;<=;2SB;6| [20]
 73   0.8   |INT;+;3$;34| [9]
160   1.8   |INT;*;3$;37| [6]
180   2.1   |ABELGRP-;*;Nni2S;3| [11]
366   4.2   |INT;rem;3$;44| [16]
369   4.2   IDENTITY [2]
407   4.7   |INT;<;2$B;31| [3]


We can see that "=$INT, +$INT, *$INT, <$INT" are not inlined, and
"*$ABELMON, *$ABELGRP, >, <=" are not defined in INT, thus impossible
to get inlined.

So I want to add inline optimization for these functions.
(I also add inline support for "positive?, even?, ^")

=== test case
)time on
)lisp (require :sb-sprof)

f := sqrt(tan(x)^2 + 2*tan(x) + 2)
g := integrate(f, x)

f2 := D(g, x)

)lisp (sb-sprof:start-profiling)
normalize(f-f2)
)lisp (sb-sprof:stop-profiling)

)lisp (sb-sprof:report)
===

Benchmark result:
 D(g, x)normalize(f-f2)
before patch40.3 s  87.4 s
after patch 28.4 s  57.3 s

So it's 30% faster, saves 40 seconds in testing.

diff --git a/src/algebra/integer.spad b/src/algebra/integer.spad
index 0e619c2f..35f673b2 100644
--- a/src/algebra/integer.spad
+++ b/src/algebra/integer.spad
@@ -59,7 +59,6 @@
ZP ==> SparseUnivariatePolynomial %
ZZP ==> SparseUnivariatePolynomial Integer
x, y : %
-  n : NonNegativeInteger

writeOMInt(dev : OpenMathDevice, x : %) : Void ==
  if x < 0 then
@@ -87,6 +86,7 @@
dec  x  == x - 1
hashUpdate!(hs, s) == update!(hs, SXHASH(s)$Lisp)$HashState
negative? x == MINUSP(x)$Lisp
+  positive? x == PLUSP(x)$Lisp
coerce(x) : OutputForm == outputForm(x pretend Integer)
coerce(m : Integer) : % == m pretend %
convert(x : %) : Integer == x pretend Integer
@@ -125,15 +125,21 @@
random(x) == RANDOM(x)$Lisp
x = y == EQL(x, y)$Lisp
x < y == (x y == (x>y)$Lisp
-- critical for coercions to NonNegativeInteger
x >= y == (x >= y)$Lisp
+  x <= y == (x <= y)$Lisp
- x == (-x)$Lisp
x + y == (x+y)$Lisp
x - y == (x-y)$Lisp
x * y == (x*y)$Lisp
(m : Integer) * (y : %) == (m*y)$Lisp -- for subsumption problem
-  x ^ n == EXPT(x, n)$Lisp
+  (m : PositiveInteger) * (y : %) == (m*y)$Lisp
+  (m : NonNegativeInteger) * (y : %) == (m*y)$Lisp
+  (x : %) ^ (n : NonNegativeInteger) == EXPT(x, n)$Lisp
+  (x : %) ^ (n : PositiveInteger) == EXPT(x, n)$Lisp
odd? x == ODDP(x)$Lisp
+  even? x == EVENP(x)$Lisp
max(x, y) == MAX(x, y)$Lisp
min(x, y) == MIN(x, y)$Lisp
divide(x, y) == DIVIDE2(x, y)$Lisp
@@ -196,6 +200,17 @@
  zero?(n := shift(n, -1)) => return y
  z := mulmod(z, z, p)

+  -- copied from IntegerNumberSystem to get inline optimization
+  symmetricRemainder(x, n) ==
+  r := x rem n
+  r = 0 => 0
+  if n < 0 then n := -n
+  r > 0 =>
+  2 * r > n => r - n
+  r
+  2 * r + n <= 0 => r + n
+  r
+
  )abbrev domain NNI NonNegativeInteger
  ++ Author:
  ++ Basic Operations:

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To post to this group, send email to fricas-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [fricas-devel] erf is left unevaluated in expression

2018-11-21 Thread Slawomir Kolodynski
It sort of helps in the sense that the hint about specifying the types is 
useful. 
I could not apply the solution directly because it lead to another very 
surprising (to me) quirk of FriCAS: lack of a basic form of referential 
transparency between variables and literals. Namely if you assign a literal 
to a variable and later in code use a variable, the result may be different 
than if you had used the literal instead. For example:

Let's assign an expression to a variable:

BS := 
((S*erf(((2*log((S/K))+T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))*erf(((2*log((S/K))-T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))+S)/2)

define an abbreviation for DoubleFloat

F:=DoubleFloat

Now define a function using the literal

C0(S:F,r:F,T:F,K:F,s:F):F == 
((S*erf(((2*log((S/K))+T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))*erf(((2*log((S/K))-T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))+S)/2)

And another one using the variable which stores that literal

C1(S:F,r:F,T:F,K:F,s:F):F == BS

Now the first function can be evaluated

C0(10.0,0.05,0.8,11.0,0.05)
0.023903294619544546

But the second cannot

C1(10.0,0.05,0.8,11.0,0.05)
Cannot convert the value from type Expression(Integer) to  DoubleFloat .

It seems the only way to create a function when the expression is stored in 
a variable is to first declare the function 

C2:(F,F,F,F,F) -> F

Then use the function operation to create the function from the expression

function(BS,'C2,['S,'r,'T,'K,'s])

Then such function can be evaluated

C2(10.0,0.05,0.8,11.0,0.05)
0.023903294619544546


On Saturday, November 10, 2018 at 12:07:44 PM UTC+1, Ralf Hemmecke wrote:

> > Is there a way to force the evaluation of erf so that the function   
> > s+->C(11.0,s)  can be plotted? 
>
> Does the following help? 
>
> The idea is that you specify the type so that the result is not 
> Expression(Float) (and must later be converted to (Double)Float, but 
> rather an element of DoubleFloat. 
>
> Ralf 
>
> F ==> DoubleFloat 
>
> C0(S:F,r:F,T:F, 
> K:F,s:F):F==((S*erf(((2*log((S/K))+T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))*erf(((2*log((S/K))-T*s^2+2*T*r)/(2*s*sqrt(2)*sqrt(T-K*%e^((-T*r))+S)/2)
>  
>
>
> C(K:F, s:F):F==C0(10.0, 0.05, 0.8, K, s) 
>
> CC(s:F):F==C(11.0, s) 
>
> draw(CC, 2.0..3.0) 
>

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To post to this group, send email to fricas-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.