result into multiple variables



> Suppose I have an operation which returns two results.  For 
> example, if I do FACTOR(10), this returns 2 and 5.  How do
> I write the program such that 2 and 5 go into the variables
> p and q?

It doesn't actually return 2 and 5; it returns the unsimplified product
of 2 and 5.  You can see this by looking at op(factor(10)) (which
returns the operator of an expression) or string(factor(10)) (which
returns a parseable linear form, including all the * operators which are
normally expressed by space on output).

So in that case, you can use the "part" function to extract the 2 and
the 5, e.g. p:part(factored,1); q: part(factored,2).  In general, to get
the list of arguments of any operator, you can use the args function:
args(factored) => [2,5]; args(a*b-c) => [a*b,-c] (Maxima represents
a*b-c as (a*b)+(-c)).

Do keep in mind that the factors may have powers (e.g. 90=2*3^2*5).  It
turns out that there's a bug (bug report 588346) in part/args/op (which
all use nformat internally) for this case; you have to use inpart
instead:

   part(factor(90,2)) => 9
   inpart(factor(90,2)) => 3^2

inargs and inop are not defined in the standard system.  Here are their
definitions:

   inargs(q):=block([inflag:true],args(q))$
   inop(q):=block([inflag:true],op(q))$

And some examples:

   args(factor(90)) => [2,9,5]
   inargs(factor(90)) => [2,3^2,5]
   quo: w*x/(y*z)
   op(quo) => "/"
   args(quo) => [w*x, y*z]
   inop(quo) => "*"
   inargs(quo) => [w, x, 1/y, 1/z]

Here is an example of using inargs(factor(...)) in a program.  Let's
suppose we want to count the number of prime factors of an integer.  Our
first stab at this might be length(factor(n)).  Sure enough,
length(factor(12)) = 2.  But it doesn't work in other cases:

   length(factor(2)) => Error
      2 is a number; numbers have no length
   length(factor(2187)) => 2
      factor(2187) = 3^7 is not a product of one factor, but
      an exponentiation: 3^7 = "^"(3,7) -- two arguments

So here is a corrected version:


   countprimefactors(n):=
      block([factored],
	 n: abs(n),
         if not ratnump(n) then error("Rationals only"),
	 if n=0 or n=1 then return(0),
	 factored: factor(n),
         if atom(factored) or inop(factored)="^" then 1
                  else length(inargs(factored)));

   countprimefactors(1) => 0
   countprimefactors(25) => 2
   countprimefactors(20!) => 8
   countprimefactors(999/77) => 4
      999/77 = 3^3 * 7^-1 * 11^-1 * 37
   makelist(countprimefactors(2^i-1),i,1,30)

Hope this helps.

       -s