Discussion:
[fricas-devel] erf is left unevaluated in expression
Slawomir Kolodynski
2018-11-10 10:22:50 UTC
Permalink
I got a problem trying to plot a function that uses the erf funtion. It
seems that the reason was that erf is sometimes left unevaluated by FriCAS
even in expressions that only involve erf and numbers.
To replicate one can define the expression like

Ce:=((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)

This contains variables S,r,T,K,s. I set the first three:

S:=10.0,r:=0.05,T:=0.8

Then I create a function C of the remaining K,s variables:

function(Ce,'C,'K,'s)

But then when I try to evaluate the function C

C(11.0,0.05)

I get an Expression(Float)

5.2843419153_377776519*erf(0.8903421181_7640573378)-5.0*erf(0.8587193415_7472194046)-0.2843419153_3777765192

I am using TEXmacs interface.

Curiously, this expression can be evaluated, sending it back in another
field returns 0.0239032946_195444183

Is there a way to force the evaluation of erf so that the function
s+->C(11.0,s) can be plotted?

Thanks,

Slawomir Kolodynski
--
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+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Ralf Hemmecke
2018-11-10 11:07:41 UTC
Permalink
Post by Slawomir Kolodynski
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+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Slawomir Kolodynski
2018-11-21 10:22:27 UTC
Permalink
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
Post by Ralf Hemmecke
Post by Slawomir Kolodynski
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+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
oldk1331
2018-11-21 13:49:45 UTC
Permalink
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,
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+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Loading...