Discussion:
[Maxima-discuss] including repeated code
Frank Muldoon
2017-07-15 13:32:11 UTC
Permalink
Hello all,

In order to reduce code replication I would like include some text into
a function. I have tried using "batch", "loadfile" and "batchload" but
have had no luck. What I am looking for is the equivalent of the
Fortran include function which inserts code into a file before any
compilation or evaluation of the file or code is carried out. I attach
a simple example.

Cheers,
Frank
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)

ЀрэМк Херберт МалЎуМ, к.ф.-ÐŒ.Ðœ.
НПвые ТехМПлПгОО О СервОс
195220 г. СаМкт-Петербург
ул. Гжатская, ÐŽ. 27, кПЌМата 205
+79313075021 (ЌПбОльМый)

fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/webpage/frank-muldoon.html
Gunter Königsmann
2017-07-15 18:05:25 UTC
Permalink
load() should work. But you have to make the file names end in .mac or in .wxm

Kind regards,

Gunter.
Post by Frank Muldoon
Hello all,
In order to reduce code replication I would like include some text into
a function. I have tried using "batch", "loadfile" and "batchload" but
have had no luck. What I am looking for is the equivalent of the
Fortran include function which inserts code into a file before any
compilation or evaluation of the file or code is carried out. I attach
a simple example.
Cheers,
Frank
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)
ЀрэМк Херберт МалЎуМ, к.ф.-ÐŒ.Ðœ.
НПвые ТехМПлПгОО О СервОс
195220 г. СаМкт-Петербург
ул. Гжатская, ÐŽ. 27, кПЌМата 205
+79313075021 (ЌПбОльМый)
fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/webpage/frank-muldoon.html
--
Diese Nachricht wurde von meinem Android-GerÀt mit K-9 Mail gesendet.
Richard Fateman
2017-07-15 20:34:13 UTC
Permalink
Post by Gunter Königsmann
load() should work. But you have to make the file names end in .mac or in .wxm
you could also try reading about autoload.

It may be useful to have something like

if fileX_loaded=true then nil else load("fileX.mac")

and in the file fileX.mac, include a line
fileX_loaded:true.

RJF
Robert Dodier
2017-07-15 22:40:23 UTC
Permalink
Post by Gunter Königsmann
load() should work. But you have to make the file names end in .mac or in .wxm
If the whole file name is given, as it is in this case, then it can have
any extension. Only if the extension is omitted must it be .mac or .lisp
(or, I guess, .wxm in wxMaxima).

best

Robert Dodier
Gunter Königsmann
2017-07-16 05:32:44 UTC
Permalink
Post by Gunter Königsmann
Post by Gunter Königsmann
load() should work. But you have to make the file names end in .mac
or in .wxm
If the whole file name is given, as it is in this case, then it can have
any extension. Only if the extension is omitted must it be .mac or .lisp
(or, I guess, .wxm in wxMaxima).
In the newest version .wxm files can be loaded from plain Maxima, too.
--
Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.
Frank Muldoon
2017-07-17 11:25:31 UTC
Permalink
Hello Gunter,

Thanks for the suggestion. That does not seem to work however, as it
throws the following error:

(%i4) tmp:0
a.maxima:4:13:incorrect syntax: Missing )
load("a.mac")$

I attach a very simple example.

Cheers,
Frank
Post by Gunter Königsmann
load() should work. But you have to make the file names end in .mac or in .wxm
Kind regards,
Gunter.
Am 15. Juli 2017 15:32:11 MESZ schrieb Frank Muldoon
Hello all,
In order to reduce code replication I would like include some text into
a function. I have tried using "batch", "loadfile" and "batchload" but
have had no luck. What I am looking for is the equivalent of the
Fortran include function which inserts code into a file before any
compilation or evaluation of the file or code is carried out. I attach
a simple example.
Cheers,
Frank
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)

ЀрэМк Херберт МалЎуМ, к.ф.-ÐŒ.Ðœ.
НПвые ТехМПлПгОО О СервОс
195220 г. СаМкт-Петербург
ул. Гжатская, ÐŽ. 27, кПЌМата 205
+79313075021 (ЌПбОльМый)

fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/webpage/frank-muldoon.html
Stavros Macrakis (Σταῦρος Μακράκης)
2017-07-17 12:47:52 UTC
Permalink
"load" and the like do not perform *textual insertion*. They load a file of
syntactically complete definitions and statements.

There is no function for textual insertion à la "#include" in C in Maxima.

For your case, that shouldn't be a problem, since Maxima has dynamic scope.
There is also no need for a separate file:


mycond23() := if ii=-1 then ni:im1 elseif ii= 0 then ni:i
elseif ii= 1 then ni:ip1$

f(...) := ... do ( mycond23(),
tmp: tmp + q(a,ni) ...)$

If mycond23 is used in multiple files, it is easy enough to pull it out
into a separate load file.

By the way, mycond23 can more simply be written as

mycond23() := ni: if ii=-1 then im1 elseif ii= 0 then i elseif
ii= 1 then ip1$

or even

mycond23() := [im1,i,ip1][ii+2]

This last version has the advantage of giving an error if ii is not -1, 0,
or 1.

mycond23 could also, of course, be written as a macro:

mycond23() ::= '(ni: if ii=-1 then im1 elseif ii= 0 then i elseif
ii= 1 then ip1)$
Post by Frank Muldoon
Hello Gunter,
Thanks for the suggestion. That does not seem to work however, as it
(%i4) tmp:0
a.maxima:4:13:incorrect syntax: Missing )
load("a.mac")$
I attach a very simple example.
Cheers,
Frank
Post by Gunter Königsmann
load() should work. But you have to make the file names end in .mac or in .wxm
Kind regards,
Gunter.
Am 15. Juli 2017 15:32:11 MESZ schrieb Frank Muldoon
Hello all,
In order to reduce code replication I would like include some
text into
Post by Gunter Königsmann
a function. I have tried using "batch", "loadfile" and
"batchload" but
Post by Gunter Königsmann
have had no luck. What I am looking for is the equivalent of the
Fortran include function which inserts code into a file before
any
Post by Gunter Königsmann
compilation or evaluation of the file or code is carried out. I
attach
Post by Gunter Königsmann
a simple example.
Cheers,
Frank
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)
ЀрэМк Херберт МалЎуМ, к.ф.-ÐŒ.Ðœ.
НПвые ТехМПлПгОО О СервОс
195220 г. СаМкт-Петербург
ул. Гжатская, ÐŽ. 27, кПЌМата 205
+79313075021 (ЌПбОльМый)
fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/
webpage/frank-muldoon.html
------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Maxima-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/maxima-discuss
Frank Muldoon
2017-07-17 20:29:24 UTC
Permalink
Hello Stavros,

Thanks very much for the note and suggestions. I was indeed
misunderstanding the functionality of "load" and so forth. Your last
suggestion seems to be most concise, and considering the very useful
error trapping feature, the best approach. After modifying it as

ni : [im1,i,ip1][ii+2],

all works fine with "ni" being replaced with "im1,i,ip1" appropriately
and the additional benefit of catching out-of-bounds errors.

Cheers,
Frank


On Mon, 2017-07-17 at 08:47 -0400, Stavros Macrakis (Σταῦρος Μακράκης)
"load" and the like do not perform textual insertion. They load a file
of syntactically complete definitions and statements.
There is no function for textual insertion à la "#include" in C in
Maxima.
For your case, that shouldn't be a problem, since Maxima has dynamic
mycond23() := if ii=-1 then ni:im1 elseif ii= 0 then
ni:i elseif ii= 1 then ni:ip1$
f(...) := ... do ( mycond23(),
tmp: tmp + q(a,ni) ...)$
If mycond23 is used in multiple files, it is easy enough to pull it
out into a separate load file.
By the way, mycond23 can more simply be written as
mycond23() := ni: if ii=-1 then im1 elseif ii= 0 then i
elseif ii= 1 then ip1$
or even
mycond23() := [im1,i,ip1][ii+2]
This last version has the advantage of giving an error if ii is not
-1, 0, or 1.
mycond23() ::= '(ni: if ii=-1 then im1 elseif ii= 0 then i
elseif ii= 1 then ip1)$
Hello Gunter,
Thanks for the suggestion. That does not seem to work
however, as it
(%i4) tmp:0
a.maxima:4:13:incorrect syntax: Missing )
load("a.mac")$
I attach a very simple example.
Cheers,
Frank
Post by Gunter Königsmann
load() should work. But you have to make the file names end
in .mac or
Post by Gunter Königsmann
in .wxm
Kind regards,
Gunter.
Am 15. Juli 2017 15:32:11 MESZ schrieb Frank Muldoon
Hello all,
In order to reduce code replication I would like
include some text into
Post by Gunter Königsmann
a function. I have tried using "batch", "loadfile"
and "batchload" but
Post by Gunter Königsmann
have had no luck. What I am looking for is the
equivalent of the
Post by Gunter Königsmann
Fortran include function which inserts code into a
file before any
Post by Gunter Königsmann
compilation or evaluation of the file or code is
carried out. I attach
Post by Gunter Königsmann
a simple example.
Cheers,
Frank
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)
Фрэнк Херберт Малдун, к.ф.-м.н.
Новые Технологии и Сервис
195220 г. Санкт-Петербург
ул. Гжатская, д. 27, комната 205
+79313075021 (мобильный)
fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/webpage/frank-muldoon.html
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Maxima-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/maxima-discuss
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--
Frank Herbert Muldoon, Ph.D. Mechanical Engineering
New Technologies and Service
27 Gzhatskaya street, room 205
Saint Petersburg
Russia, 195220
+79313075021 (cell)

Фрэнк Херберт Малдун, к.ф.-м.н.
Новые Технологии и Сервис
195220 г. Санкт-Петербург
ул. Гжатская, д. 27, комната 205
+79313075021 (мобильный)

fmuldoo (skype)
http://tetra.fluid.tuwien.ac.at/fmuldoo/public_html/webpage/frank-muldoon.html
Robert Dodier
2017-07-15 22:38:58 UTC
Permalink
Post by Frank Muldoon
In order to reduce code replication I would like include some text into
a function. I have tried using "batch", "loadfile" and "batchload" but
have had no luck. What I am looking for is the equivalent of the
Fortran include function which inserts code into a file before any
compilation or evaluation of the file or code is carried out. I attach
a simple example.
For better or worse Maxima is sensitive to the presence or absence of
delimiters such as commas, semicolons, and dollar signs. I looked at the
code you posted and I see that by fixing up the delimiters as shown
below, the script a.maxima runs successfully. I didn't try to determine
whether or not the outputs are correct.

Note that each file loaded by batch or load must contain only complete
expressions -- Maxima batch and load are not purely textual substitution
mechanisms.

About Maxima functions, a Maxima function returns the last expression
evaluated. It isn't necessary to say return(whatever) or
fnname:whatever. Just let whatever be the last thing evaluated in the
function.

About Lagrange interpolation, there is a function lagrange in the share
package interpol. See ? lagrange for documentation about it.

Hope this helps,

Robert Dodier

PS. Here are the changes to fix up delimiters. "-" marks the original
line and "+" marks the new line.

$ diff -U 0 lagrange-n-point-3d-interpolant-01.maxima-original lagrange-n-point-3d-interpolant-01.maxima
--- lagrange-n-point-3d-interpolant-01.maxima-original 2017-07-15 15:13:48.124525790 -0700
+++ lagrange-n-point-3d-interpolant-01.maxima 2017-07-15 15:12:06.536022041 -0700
@@ -41 +41 @@
-batch("basic-periodic-3d-lagrange-interpolant-code-01.maxima")$
+batch("basic-periodic-3d-lagrange-interpolant-code-01.maxima"),

$ diff -U 0 basic-periodic-3d-lagrange-interpolant-code-01.maxima-original basic-periodic-3d-lagrange-interpolant-code-01.maxima
--- basic-periodic-3d-lagrange-interpolant-code-01.maxima-original 2017-07-15 15:14:27.336720234 -0700
+++ basic-periodic-3d-lagrange-interpolant-code-01.maxima 2017-07-15 15:15:41.033085691 -0700
@@ -9 +9 @@
-elseif kk= 1 then nk:kp1 elseif kk= 2 then nk:kp2 elseif kk= 3 then nk:kp3,
+elseif kk= 1 then nk:kp1 elseif kk= 2 then nk:kp2 elseif kk= 3 then nk:kp3;
Continue reading on narkive:
Loading...