login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for PascalTriangle revision 1 of 2

1 2
Editor: test1
Time: 2014/02/19 18:45:11 GMT+0
Note:

changed:
-
Below there is a function which generates Pascal triangle by iteratively filling
rows of a matrix using recurence formula.  Spad array indexing is one based,
so we cheat and add 1 to indices.
\begin{spad}
)abbrev package PASCAL Pascal
Pascal : with ( make_pascal : Integer -> Matrix(Integer) )
  == add
    make_pascal(N : Integer) : Matrix(Integer) ==
        -- coerce here to NonNegativeInteger because this is what 'zero' requires
        nn := N::NonNegativeInteger
        tab := zero(nn, nn)$Matrix(Integer)
        n : Integer
        tab(1, 1) := 1
        for n in 2..nn repeat
            -- Fill row numer n
            k : Integer
            tab(n, 1) := 1
            tab(n, n) := 1
            for k in 2..(n - 1) repeat
                tab(n, k) := tab(n-1, k) + tab(n-1, k-1)
        tab
\end{spad}

Try it out:
\begin{axiom}
make_pascal(5)
\end{axiom}

See also PascalTriangleNopile.

Below there is a function which generates Pascal triangle by iteratively filling rows of a matrix using recurence formula. Spad array indexing is one based, so we cheat and add 1 to indices.

spad
)abbrev package PASCAL Pascal
Pascal : with ( make_pascal : Integer -> Matrix(Integer) )
  == add
    make_pascal(N : Integer) : Matrix(Integer) ==
        -- coerce here to NonNegativeInteger because this is what 'zero' requires
        nn := N::NonNegativeInteger
        tab := zero(nn, nn)$Matrix(Integer)
        n : Integer
        tab(1, 1) := 1
        for n in 2..nn repeat
            -- Fill row numer n
            k : Integer
            tab(n, 1) := 1
            tab(n, n) := 1
            for k in 2..(n - 1) repeat
                tab(n, k) := tab(n-1, k) + tab(n-1, k-1)
        tab
spad
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/2120495552417862399-25px001.spad
      using old system compiler.
   PASCAL abbreviates package Pascal 
------------------------------------------------------------------------
   initializing NRLIB PASCAL for Pascal 
   compiling into NRLIB PASCAL 
   compiling exported make_pascal : Integer -> Matrix Integer
Time: 0.03 SEC.
(time taken in buildFunctor: 0)
;;; *** |Pascal| REDEFINED
;;; *** |Pascal| REDEFINED Time: 0 SEC.
Cumulative Statistics for Constructor Pascal Time: 0.03 seconds
finalizing NRLIB PASCAL Processing Pascal for Browser database: --->-->Pascal(constructor): Not documented!!!! --->-->Pascal((make_pascal ((Matrix (Integer)) (Integer)))): Not documented!!!! --->-->Pascal(): Missing Description ; compiling file "/var/aw/var/LatexWiki/PASCAL.NRLIB/PASCAL.lsp" (written 19 FEB 2014 06:45:11 PM):
; /var/aw/var/LatexWiki/PASCAL.NRLIB/PASCAL.fasl written ; compilation finished in 0:00:00.024 ------------------------------------------------------------------------ Pascal is now explicitly exposed in frame initial Pascal will be automatically loaded when needed from /var/aw/var/LatexWiki/PASCAL.NRLIB/PASCAL

Try it out:

fricas
make_pascal(5)

\label{eq1}\left[ 
\begin{array}{ccccc}
1 & 0 & 0 & 0 & 0 
\
1 & 1 & 0 & 0 & 0 
\
1 & 2 & 1 & 0 & 0 
\
1 & 3 & 3 & 1 & 0 
\
1 & 4 & 6 & 4 & 1 
(1)
Type: Matrix(Integer)

See also PascalTriangleNopile?.