Jump to content

Maxima/Numbers

From Wikibooks, open books for an open world

Check

[edit | edit source]
(%i1) load(to_poly);
(%o1)  home/a/maxima/share/to_poly_solve/to_poly.lisp
(%i2) r;
(%o2) r 
(%i3) isreal_p(r);
(%o3) true     /*  When I use unspecified variables, Maxima seems to assume that they are real */ 
(%i4) z:x+y*%i;
(%o4) %i y + x
(%i5) isreal_p(z);
(%o5) isreal_p(%i y) /* maxima can't check if it is real or not */
(%i6) isreal_p(x);
(%o6) true
(%i7) isreal_p(y);
(%o7) true
(%i8) complex_number_p(z); 
(%o8) false 
(%i9) declare(z, complex);
(%o9) done
(%i10) complex_number_p(z);
(%o10)   false /* still not complex */                                                                                                              true


Random numbers

[edit | edit source]
  bfloat(random(10^fpprec) / 10^fpprec); /* random bfloat with fpprec decimal digits in the range 0 to 1 */

Number types

[edit | edit source]

Binary numbers

[edit | edit source]
(%i1) ibase;
(%o1) 10
(%i2) obase;
(%o2) 10
(%i3) ibase:2;
(%o3) 2
(%i4) x=1001110;
(%o4) x=78


Complex numbers

[edit | edit source]

Argument

[edit | edit source]

Principial value of argument of complex number in turns carg produces results in the range (-pi, pi] . It can be mapped to [0, 2*pi) by adding 2*pi to the negative values

carg_t(z):=
 block(
 [t],
 t:carg(z)/(2*%pi),  /* now in turns */
 if t<0 then t:t+1, /* map from (-1/2,1/2] to [0, 1) */
 return(t)
)$

On can order list of complex points according to it's argument :

l2_ordered: sort(l2, lambda([z1,z2], is(carg(z1) < carg(z2))))$


rational numbers

[edit | edit source]
  • rat
  • ratp

Rationalize with limit denominator:[1]

limit_denominator(x, max_denominator):=
block([p0, q0, p1, q1, n, d, a, q2, k, bound1, bound2, ratprint: false],
 [p0, q0, p1, q1]: [0, 1, 1, 0],
 [n, d]: ratexpand([ratnum(x), ratdenom(x)], 0),
 if d <= max_denominator then x else
 (catch(
   do block(
     a: quotient(n, d),
     q2: q0+a*q1,
     if q2 > max_denominator then throw('done),
     [p0, q0, p1, q1]: [p1, q1, p0+a*p1, q2],
     [n, d]: [d, n-a*d])),
 k: quotient(max_denominator-q0, q1),
 bound1: (p0+k*p1)/(q0+k*q1),
 bound2: p1/q1,
 if abs(bound2 - x) <= abs(bound1 - x) then bound2 else bound1))$

Predicate functions

[edit | edit source]
(%i1) is(0=0.0);
(%o1) false

Compare :

(%i1) a:0.0$
(%i2)is(equal(a,0));
(%o2) true


List of predicate functions ( see p at the end ) :

  • abasep
  • alphacharp
  • alphanumericp
  • atom
  • bfloatp
  • blockmatrixp
  • cequal
  • cequalignore
  • cgreaterp
  • cgreaterpignore
  • charp
  • clessp
  • clesspignore
  • complex_number_p from to_poly package
  • constantp
  • constituent
  • diagmatrixp
  • digitcharp
  • disjointp
  • elementp
  • emptyp
  • evenp
  • featurep
  • floatnump ( compare : isreal_p)
  • if
  • integerp
  • intervalp
  • is
  • isreal_p from to_poly package
  • lcharp
  • listp
  • listp
  • lowercasep
  • mapatom
  • matrixp
  • matrixp
  • maybe
  • member
  • nonnegintegerp
  • nonscalarp
  • numberp
  • oddp
  • operatorp
  • ordergreatp
  • orderlessp
  • picture_equalp
  • picturep
  • poly_depends_p
  • poly_grobner_subsetp
  • polynomialp
  • prederror
  • primep
  • ratnump
  • ratp
  • scalarp
  • sequal
  • sequalignore
  • setequalp
  • setp
  • stringp
  • subsetp
  • subvarp
  • symbolp
  • symmetricp
  • taylorp
  • unknown
  • uppercasep
  • zeroequiv
  • zeromatrixp
  • zn_primroot_p


See also :

  • declare[2]
    • property:
      • rational, irrational, real, imaginary, complex,
      • even, odd,
      • decreasing, increasing
      • evenfun, oddfun

Number Theory

[edit | edit source]

There are functions and operators useful with integer numbers

Elementary number theory

[edit | edit source]

In Maxima there are some elementary functions like the factorial n! and the double factorial n!! defined as where is the greatest integer less than or equal to

Divisibility

[edit | edit source]

Some of the most important functions for integer numbers have to do with divisibility:

gcd, ifactor, mod, divisors... 

all of them well documented in help. you can view it with the '?' command.

Function ifactors takes positive integer and returns a list of pairs : prime factor and its exponent. For example :

a:ifactors(8);
[[2,3]]

It means that :

Other Functions

[edit | edit source]

Continuus fractions :

(%i6) cfdisrep([1,1,1,1]);
(%o6) 1+1/(1+1/(1+1/1))
(%i7) float(%), numer;
(%o7) 1.666666666666667
  1. stackoverflow question : rationalize-number-with-limit-denominator
  2. maxima manual : Category Declarations-and-inferences