Online Algebra Calculator
Many of users do not use powerful Maxima computer algebra system for systematic work, but for ad hoc algebraic calculations – equations, functions, matrixes, etc. only. To do this job, it is not useful to download the whole application from a web site.
Function: define_variable (<name>, <default_value>, <mode>) Introduces a global variable into the Maxima environment. define_variable is useful in user-written packages, which are often translated or compiled.
![Define_variable Example define_variable(n,0.3,float);
define_variable(h,0.175,float);
f(x,e):=(mode_declare([x,e],float),n*exp(x)/(e+x^(2*h)*exp(h*x)));
compile(f);
array([x,e],float,35);
init_float_array(x,1.0e-3,6.85);
e[0]:5.0;
runge_kutta(f,x,e);
graph2(x,e);](http://maxima-online.org//plot.html?g=i-1169018735.png&t=img&db=r1178181357)
define_variable carries out the following steps:
1. mode_declare (<name>, <mode>) declares the mode of <name> to the translator. See mode_declare for a list of the possible modes.
2. If the variable is unbound, <default_value> is assigned to <name>.
3. declare (<name>, special) declares it special.
4. Associates <name> with a test function to ensure that <name> is only assigned values of the declared mode.
The value_check property can be assigned to any variable which has been defined via define_variable with a mode other than any. The value_check property is a lambda expression or the name of a function of one variable, which is called when an attempt is made to assign a value to the variable. The argument of the value_check function is the would-be assigned value.
define_variable evaluates default_value, and quotes name and mode. define_variable returns the current value of name, which is default_value if name was unbound before, and otherwise it is the previous value of name.
Examples:
foois a Boolean variable, with the initial valuetrue. (%i1) define_variable (foo, true, boolean); (%o1) true (%i2) foo; (%o2) true (%i3) foo: false; (%o3) false (%i4) foo: %pi; Error: foo was declared mode boolean, has value: %pi -- an error. Quitting. To debug this try debugmode(true); (%i5) foo; (%o5) false
bar is an integer variable, which must be prime.
(%i1) define_variable (bar, 2, integer);
(%o1) 2
(%i2) qput (bar, prime_test, value_check);
(%o2) prime_test
(%i3) prime_test (y) := if not primep(y) then
error (y, "is not prime.");
(%o3) prime_test(y) := if not primep(y) then error(y, "is not prime.")
(%i4) bar: 1439;
(%o4) 1439
(%i5) bar: 1440;
1440 is not prime.
#0: prime_test(y=1440)
-- an error. Quitting. To debug this try debugmode(true);
(%i6) bar;
(%o6) 1439 baz_quux is a variable which cannot be assigned a value. The mode any_check is like any, but any_check enables the value_check mechanism, and any does not.
(%i1) define_variable (baz_quux, baz_quux, any_check);
(%o1) baz_quux
(%i2) F: lambda ([y], if y # baz_quux then
error ("Cannot assign to baz_quux."));(%o2) lambda([y], if y # baz_quux
then error(Cannot assign to baz_quux.))
(%i3) qput (baz_quux, F, value_check);
(%o3) lambda([y], if y # baz_quuxthen error(Cannot assign tobaz_quux.)) (%i4) baz_quux: baz_quux; (%o4) baz_quux (%i5) baz_quux: sqrt(2); Cannot assign tobaz_quux. #0: lambda([y],if y # baz_quux then error("Cannot assign tobaz_quux."))(y=sqrt(2)) -- an error. Quitting. To debug this try debugmode(true); (%i6) baz_quux; (%o6) baz_quux
(%o1) true (%i2)