compile_file() creates some output files. None of these can be used with load() or loadfile() or batch() or can from the shell commandline. And there is no example how one can use them.
Here is an example from the test suite.
(kill (all),
lisp_name : ssubst ("_", " ", build_info()#lisp_name),
maxima_filename : sconcat (maxima_tempdir, "/tmp-rtest_translator-compile_file-", lisp_name, ".mac"),
fasl_filename : sconcat (maxima_tempdir, "/tmp-rtest_translator-compile_file-", lisp_name, ".fasl"),
lisp_filename : sconcat (maxima_tempdir, "/tmp-rtest_translator-compile_file-", lisp_name, ".LISP"),
maxima_output : openw (maxima_filename),
maxima_content :
"foo (x) := my_foo * x;
Foo (x) := my_Foo * x;
FOO (x) := my_FOO * x;
[my_foo, my_Foo, my_FOO] : [123, 456, 789];
results : [foo (2), Foo (3), FOO (4)];
my_test () := is (results = [2*123, 3*456, 4*789]);",
printf (maxima_output, maxima_content),
close (maxima_output),
compile_file (maxima_filename, fasl_filename, lisp_filename),
kill (allbut (lisp_filename)),
load (lisp_filename),
my_test ());
Related
I have the following functions:
P[t_] := P[t] = P[t-1] +a*ED[t-1];
ED[t_] := ED[t] = DF[t] + DC[t];
DF[t_] := DF[t] = b (F - P[t]);
DC[t_] := DC[t] = c (P[t] - F);
And the following parameters:
a=1;
c=0.2;
b = 0.75;
F=100;
In Mathematica I use the function "ListLinePlot" in order to plot P[t] and F:
ListLinePlot[{Table[P[t], {t, 0, 25}], Table[F, {t, 0, 25}]}, PlotStyle → {Black, Red},Frame → True, FrameLabel → {"time", "price"}, AspectRatio → 0.4, PlotRange → All]
How can I do this in wxMaxima? Is there a similar function or an alternative to ListLinePlot?
This is my attempt in wxMaxima:
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*[F-P[t]];
DC[t] := c*[P[t]-F];
a=1;
c=0.2;
b=0.75;
F=100;
And then I tried:
draw2d(points(P[t], [t,0,25]))
The plotted function should look like this:
OK, I've adapted the code you showed above. This works for me. I'm working with Maxima 5.44 on macOS.
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*(F-P[t]);
DC[t] := c*(P[t]-F);
a:1;
c:0.2;
b:0.75;
F:100;
P[0]: F + 1;
Pt_list: makelist (P[t], t, 0, 25);
load (draw);
set_draw_defaults (terminal = qt);
draw2d (points_joined = true, points(Pt_list));
Notes. (1) There needs to be a base case for the recursion on P. I put P[0]: F + 1. (2) Assignments are : instead of =. Note that x = y is a symbolic equation instead of an assignment. (3) Square brackets [ ] are only for subscripts and lists. Use parentheses ( ) for grouping expressions. (4) Syntax for draw2d is a little different, I fixed it up. (I put a default for terminal since the built-in value is incorrect for Maxima on macOS; if you are working on Linux or Windows, you can omit that.)
EDIT: Try this to draw a horizontal line as well.
draw2d (points_joined = true, points(Pt_list),
color = red, points([[0, F], [25, F]]),
yrange = [F - 1, P[0] + 1]);
I have an example of a code and not sure what way is the best to use.
For example I have
if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
...
check only
if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3
i am not sure if i am explaining the best but hopefuly people will understand.
I want to know if this is possible or the best way to do it. Keeping in mind that rather than in example where its x, y, z and abc. there can be more in my use.
I currently have structure as...which i dont think is practical, and think theres a better way but i am not sure
if (abc = false) then
begin
if (x = 1) and (y = 2) and (z = 3) then
begin
...
end
else
begin
if (x = 1) and (y = 2) then
begin
...
Thanks in advance
I think you're looking for or. Now you will check that x must be 1, y must be 2, and if abc is false, z must be 3.
If abc = true, z can still be three, but it won't be checked.
Note that I just wrote abc instead of abc = true. Since it's a Boolean (true/false) already, that's allowed.
Also note how the operations are grouped using parentheses. The total sub-expression abc or (z=3) must return true for the total expression to return true.
Furthermore the sequence of the terms is significant - they are evaluated left-to-right. If the term (abc or (z=3)) is replaced by the logically-equivalent term ((z=3) or abc) then z=3 will be evaluated.
if (x = 1) and (y = 2) and (abc or (z = 3)) then
// Your magic goes here
Test program body to prove sequence is important
function z : Integer;
begin
writeln('Z being evaluated');
result := x + y;
end;
begin
x := 1;y := 2;
abc := true;
if (x=1) and (y=2) and (abc or (z=3)) then
writeln ('evaluated true')
else
writeln ('evaluated false');
writeln('done');
readln;
end.
Neither of your code samples compile, because neither is using the proper syntax.
This should get you started:
if (x = 1) and (y = 2) then
begin
if (abc) then
// Handle abc = True
else
begin
if (z = 3) then
// Handle abc = false and z = 3
else
// Handle abc = false and z <> 3
end;
end;
I would like to expand in taylor series a function of type : f(x+f(x)) around x=a in the case where f(a)=0.
(%i1) atvalue(f(x),[x=a],0)$
The direct calculus yields :
(%i2) taylor(f(x+f(x)),x,a,2);
(%o2)/T/ f(a)+(at('diff(f(f(x)+x),x,1),x=a))*(x-a)+((at('diff(f(f(x)+x),x,2),x=a))*(x-a)^2)/2+...
If I define a intermediate function :
(%i3)define(tf(x),taylor(f(x),x,a,2))$
Then a expand in Taylor series I get :
(%i4) taylor(f(x+tf(x)),x,a,2);
(%o4) 0+...
I expect the following result :
f(1+f'(a))f'(a)(x-a)+(x-a)^2 f''(a)[f'(a)+(1+f'(a))^2/2]+o(x-a)^2
How could I solve this problem?
You can use gradef to simplify notation.
gradef(f(x), f1(x)) $
gradef(f1(x), f2(x)) $
atvalue(f(x), x = a, 0) $
e: f(x+f(x)) $
e: taylor(e, x, a, 2) $
e: expand(e, 0, 0)$ /* 'taylor' form to ordinar expression */
e: ev(e, nouns); /* f(a) to 0 */
returns
2 2
(f1 (a) f2(a) + 3 f1(a) f2(a) + f2(a)) (x - a)
(%o7) -----------------------------------------------
2
2
+ (f1 (a) + f1(a)) (x - a)
A solution is as follow :
gradef(f(x), f1(x)) $
gradef(f1(x), f2(x)) $
atvalue(f(x), x = a, 0) $
e: f(x+f(x)) $
e: taylor(e, x, a, 2) $
e: expand(e, 0, 0)$ /* 'taylor' form to ordinar expression*/
e: ev(e, nouns); /* f(a) to 0 */
taylor(e,x,a,2); /* Becomes again a taylor serie which could be reused*/
For example, if I want to find the order of the Steffensen method which is defined, for function f which is C^2 and f(a)=0,f'(a)!=0, by :
Sf(x)=x-f(x)^2/(f(x+f(x)-f(x))
If I straight expand this function around a I get :
Sf(x)=a+(x-a)-(f1(a)^2*(x-a)^2)/f(a)+...
which diverges since f(a)=0.
So this must be proceed in two steps. First I expand the denominator :
den:f(x+f(x))-f(x)$
t:taylor(den,x,a,2);
t: expand(t, 0, 0)$
t: ev(t, nouns)$
t:taylor(t,x,a,2);
Then I expand the function Sf:
Sf:x-f(x)^2/(t)$/*Introducing the taylor serie of den*/
taylor(Sf,x,a,2);
which provides the wanted result :
Sf(x)=a+((f1(a)+1)*f2(a)*(x-a)^2)/(2*f1(a))+...
I found that sometimes I must give types explicitly for pattern variables, otherwise Rascal would not work as expected. The following session in the Console says it all:
rascal>data foo = bar(int);
ok
rascal>int x = 1;
int: 1
rascal>[x | bar(x) <- [bar(2), bar(3)]];
list[void]: []
rascal>[x | bar(int x) <- [bar(2), bar(3)]];
list[int]: [2,3]
Why did this happen?
In the current version of Rascal it is such that variables in patterns that exist in the surrounding scope are not matched and shadowed, but rather checked for equality.
So:
<int x, x> := <2,2> => true // x is first introduced and then checked for equality
<int x, x> := <2,3> => false // x is first introduced and then checked for equality
{ int x = 1; if (x := 2) println("true"); else println("false"); // false!
And this holds for all places where we use pattern matching.
We have had several complaints about this particular design of "non-linear matching", and we intend to add an operator soon ($) to identify the intention of taking something from the surround scope. If the operator is not used, then shadowing will occur:
<int x, $x> := <2,2> => true // x is first introduced and then checked for equality
<int x, $x> := <2,3> => false // x is first introduced and then checked for equality
<int x, x> := <2,3> // static error due to illegal shadowing
<int x, y> := <2,3> => true // x and y are both introduced
{ int x = 1; if ($x := 2) println("true"); else println("false"); // false!
{ int x = 1; if (x := 2) println("true <x>"); else println("false"); // true, prints 2! or perhaps a static error.
Might also add some additional power to get expressions into patterns as in:
<1, ${1 + 2 + 3}> := <1,6> // true
The built-in Mathematica command Save[file, symbol] uses FullDefinition[] to look up the definition symbol and all of the subsidiary definitions.
For example, the commands
a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]
produces the file test.dat containing
c := 2*a + b
a := b
I have a program with a lot of prettifying MakeBoxes type definitions that I do not want to be saved when I Save[] the many separate results.
In terms of the simple example above, I do not want the a := b definition saved to the file. Does anyone know a neat way to make this happen?
According to the documentation, Save uses FullDefinition while what you want is for it to use Definition. Using a Block we can override the global definition of any symbol, and in particular replace FullDefinition with Definition while running Save:
Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, c]
];
FilePrint[filename]
DeleteFile[filename]
The magic works:
c := 2*a + b
EDIT. Wrapping things up with the right attributes:
SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
FullDefinition = Definition;
Save[filename, args]];
I think
DumpSave["test1", c]
Does that.
Sample code:
a := b;
c := 2 a + b;
DumpSave["test1", c];
Clear[a, c];
<< test1
?a
?c
Out
_____________________
Global`a
_____________________
Global`c
c:=2 a+b
Warning - Warning - I don't know what I am doing
Just found this browsing the help system randomly.
Never before used RunThrough ... anyway seems to do what you want.
Clear["Global`*"];
a := b;
c := 2 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt", Unevaluated[Put[Definition[c], "c:\\rtout"]]]
FilePrint[outputfile]
Clear[a, c];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
Out
c := 2*a + b
_______________________________
Global`c
c:=2 a+b
Edit.. Works on lists with a little Hold-Fu
Clear["Global`*"];
(*Trick here *)
f[l_] := Definition ## HoldPattern /# Unevaluated#l;
SetAttributes[f, HoldFirst];
a := b;
c := 2 a + b;
d := 3 a + b;
mathcommand = StringReplace[First[$CommandLine], "MathKernel" -> "math"];
outputfile = "c:\\rtout";
RunThrough[mathcommand <> " -noprompt",Unevaluated[Put[Evaluate[f#{c, d}], "c:\\rtout"]]]
(* test *)
FilePrint[outputfile]
Clear[a, c, d];
<< "c:\\rtout"
DeleteFile[outputfile]
?c
?d