Here’s how one runs it from an interactive read-eval-print command loop in Maxima:
(%i14) block([modulus:102], rat(3)^100000);
Warning: assigning 102, a non-prime, to ‘modulus’
(%o14)/R/ 69
I have never personally run Maxima from a bash or windows command prompt, but I understand that it is possible by (essentially) sneaking stuff into the file that Maxima uses when it starts up (maxima.bat). In which case outputting to std might require using “print” or “format”… something like command line promptmaxima ? block([modulus:102], print(rat(3)^100000), exit());
Doing arithmetic modulo 102 which has factors 2, 3, 17 results in a warning,as shown above. I suppose that message would go to stdout or stderr if that is available, from a command line.
If you really want to do bignum arithmetic, any Common Lisp will support that, and may use the highly-tuned GMP library available for different architectures.
A reasonable Lisp program for powermod(n,power,modulus) would be a few lines of code, using about log_2(power) multiplies and remainders.
In some Lisps, a newly-defined program is run interpretively but can be compiled on request. In other Lisps it is immediately compiled. Lisps allow, but do not require, type declarations and hints for optimization (like in-line expansion).
I hope that some of the lessons from Common Lisp design and implementation have been considered by people implementing Julia.
~ $ time maxima --very-quiet --batch-string=''
real 0m0.089s
user 0m0.079s
sys 0m0.017s
~ $ time julia -e ''
real 0m0.438s
user 0m0.421s
sys 0m0.140s
~ $ time maxima --very-quiet --batch-string='block([modulus:102], print(rat(3)^10000000000000), exit());'
block([modulus:102],print(rat(3)^10000000000000),exit())
warning: assigning 102, a non-prime, to 'modulus'
69
exit()
real 0m0.092s
user 0m0.077s
sys 0m0.022s
~ $ time julia -e 'println( powermod(3, 10000000000000, 102) )'
69
real 0m0.545s
user 0m0.552s
sys 0m0.116s
If I see it the right way Julia could spawn Maxima on this one instead of itself to get the result faster instead of calculating it by itself - or is there something wrong with this conclusion?
Spawning julia is slow, so spawning Julia to do a single small arithmetic calculation will be inefficient. If that’s how you want to use Julia, you are better off with bc.
That’s not how other people typically use, Julia, however—or, for that matter, how they use most other programming languages, including Python. They spawn Julia (or whatever program) once and then do lots of calculations. For this pattern, tools like @btime and timeit are more useful and revealing, because they measure the marginal cost of a single small operation.
May you provide a code example which demonstrates that Julia will be faster considering the default use case you mention compared to Maxima? Just mentioning that the demonstrated timing is not representative would be much more expressive along with providing evidence, wouldn’t it?
Sorry … I have trouble with both, Julia and Maxima which are new to me, failing on constructing appropriate examples of code myself.
If I see it right Julia spawning Maxima and getting the result from Maxima will be in the total time still faster then letting Julia itself calculating it … or have I overseen something comparing the timings?
From what I can see in the timings Julia time for performing the calculation minus the time for spawning Julia is in the order of 0.1 second, where time for performing the calculation including spawning Maxima is not much but still slightly less than that. And comparing the pure calculation time suggests that Maximal is an order of magnitude faster on this one. Or is there anything I have erroneously overseen here?
First of all, almost none of Maxima is being used. A Common Lisp system and one program could do this task. I think this program would work…
(defun powermod(n pow mod)
(cond((= pow 0) 1)
((= pow 1)(rem n mod))
((evenp pow)(powermod (rem (* n n) mod) (ash pow -1)mod))
(t (rem (* n (powermod n (1- pow) mod)) mod))))
[sorry, the indentation was removed by “discourse”.]
If you want to leave a Maxima or a Lisp client sitting around, that would eliminate most of the start-up time. It works for me.
It seems to me that the routine “Oh, I just wrote a program in language X. I’ll have to start up an X-compiler-environment to run it” is turning the clock back 60 years, to pre-timesharing computing. It helps that computers are so much faster, and we don’t use punched cards… but
read-eval-print loops and interactive computing has made me more productive, I think. If we are losing that, and today programmers edit some text interactively, and then essentially fire up a batch processing system… maybe start a browser, download an app on a phone, connect to a cloud…maybe use some elaborate deployment strategy… doesn’t seem like much fun. Oh well, times change.
RJF
Surely. Most of the functions you call in julia will be compiled the first time you call it. This goes for powermod, and for run, which you would use to spawn maxima. After powermod has been compiled, I’ve shown you elsewhere that the actual runtime for powermod is around 200 nanoseconds (it’s really just a few hundred cpu cycles). After run has been compiled (it might take longer than to compile powermod) it will take some milliseconds to start maxima.
So using maxima to perform this calculation from julia would most likely take longer time.
Now, normally you don’t do just a single calculation. If you want to do, say, one million, it will take milliseconds with julia, and 10 minutes or so if you spawn maxima a million times.
Wrap your code in triple backticks to get proper formatting and monospace font.
Using a REPL is the idiomatic way to use Julia. Starting Julia from the command line to run I small piece of code is contrary to that, I didn’t catch why the OP wants to do it.