programming examples: maxima book ( 'use maxima commands')
peter.vlasschaert@gmail.com,19/06/2019

internet :
======

info:http://michel.gosse.free.fr/documentation/fichiers/maximabook.pdf

factorial : without  block statement 
==========================

recursion
=======

f1(x) := if x < 1 then 1 else x*f1(x-1)$

output :
=====

f1(5);

x;

factorial :  block statement 
==========================

no recursion
==========

f2(x):= block([temp],
                       temp:1 /*init*/,
    while (x >1)   do (
    temp:x*temp, x:x-1), /*end while*/
temp /*output*/);

output :
=====

f2(5);

x;

block combination output : 'return statement' and counter.

f3(x):= block([temp,n],
                       n : 1/*init counter */,
                       temp:1 /*init*/,
    while (x >1)   do (
        n:n+1/*update counter*/,
    temp:x*temp, x:x-1), /*end while*/
return([temp,n]) /*output*/);

output : list 
========

f3(5);

block statement combination with print
=============================

example : test

test(x1,y1):= block([],
if x1 > y1 then print (x1, " > ",y1) else
     print( x1,"is not greater",y1)
,return(alldone))$

test(4,3);

test(3,4);

x1;
y1;

how to use "assume"

assume(y>0);

test(y^2+1,-y);

for get assumption for y.
==================

forget(y>0);

how to get input list as argument of block function
======================================
combination output : print and return 'both'

[l] : input list
=========

prog1([l]):= block([],
    print ("l is bound to",l," and l[1]",l[1]),
    return(length(l)));

output
=====

prog1(a,b,c,d,e);

x;

numerical methods: newton method
===========================
theory :x = x - f(x)/diff(f(x),x)
x : x0 ,start value,iteration process
eps : precission for computation to stop.
(control : while structure: newton1)
(control : loop  structure: newton2)
expr: f(x)

expr : h
======

h:expand((x-1)*(x-3)*(x-5));

code : while structure
=================

newton1(expr,var,x0,eps) := block([xn,s,numer],
    numer: true,
    s:diff(expr,var),
    xn : x0 ,
    while abs(subst(xn,var,expr))  > eps
    do(
        m:subst(xn,var,s),
    xn:xn-subst(xn,var,expr)/m),
    return(xn));

rem :xn,s,numer  'local'

output : newton1
============

newton1(h,x,3.5,1.0e-10);

code :loop structure
=================
loop, ........................, go loop
escape from 'loop' : if and return statement

newton2(expr,var,x0,eps) := block([xn:x0,s:diff(expr,var),numer:true],
    define(f(var),expr),
    define(fprime(var),s),
    loop, if abs(f(xn)) < eps then return(xn),
      xn:xn-f(xn)/fprime(xn), go(loop) );

output : newton2
============

newton2(h,x,3.5,1.0e-10);

local :xn

xn;

check values :xn
=============

setcheck:[xn]$
newton2(h,x,3.5,1.0e-10);

rem : define
=========

define(f(var),expr);

define(fprime(var),s);