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

Edit detail for FunWithFunctions revision 1 of 5

1 2 3 4 5
Editor: hemmecke
Time: 2011/03/30 13:04:13 GMT-7
Note:

changed:
-
\begin{spad}
)abbrev package MYEXP MyExp

MyExp(F: Ring): with
    myexp: F -> F
    myexp: (F, PositiveInteger) -> F
 == add
    myexp(x: F, n: PositiveInteger): F ==
        y: F := 1
        z: F := 1
        for i in 1..n repeat
            z := z * x
            y := y + z
        y

    myexp(x: F): F == myexp(x, 10)
\end{spad}

That's generic code and has to be compiled. Look at the types of the result.

\begin{axiom}
myexp(0.1)
myexp(0.1,4)
myexp(2,4)
myexp(2/3,4)
\end{axiom}

And now we create a domain of functions and let it behave like a ring where multiplication
is concatenation of functions and addition is... well, see below.

\begin{spad}
)abbrev domain MYFUN MyFun

MyFun(F: Field): Ring with
    apply: (%, F) -> F
    coerce: (F -> F) -> %
  == add
    Rep := F -> F
    X ==> x pretend (F->F)
    Y ==> y pretend (F->F)
    zr(f: F): F == 0
    id(f: F): F == f
    plus(x: %, y: %): % == (f: F): F +-> X(f) + Y(f)
    times(x: %, y: %): % == (f: F): F +-> X(Y f)

    0: % == zr
    1: % == id
    (x: %) + (y: %) == plus(x,y)
    (x: %) * (y: %) == times(x,y)
    coerce(x: %): OutputForm == "myfun-object"
    apply(x: %, f: F): F == X f
    coerce(fun: F -> F): % == fun
\end{spad}

\begin{axiom}
F := Fraction Integer
H := MyFun(F)
phi(f: F): F == f/2
hphi:H := phi::H
apply(hphi, 1)
a==>apply
\end{axiom}
Computing the first 3 terms of $1+\varphi(1)+\varphi(\varphi(1))$.
\begin{axiom}
1+a(hphi,1)+a(hphi,a(hphi,1))
\end{axiom}
And here we first compute $1+\varphi+\varphi^2$ and then apply this function to 1.
\begin{axiom}
hexp:=myexp(hphi,2)
apply(hexp,1)
\end{axiom}


spad
)abbrev package MYEXP MyExp
MyExp(F: Ring): with myexp: F -> F myexp: (F, PositiveInteger) -> F == add myexp(x: F, n: PositiveInteger): F == y: F := 1 z: F := 1 for i in 1..n repeat z := z * x y := y + z y
myexp(x: F): F == myexp(x, 10)
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/1162035593874464771-25px001.spad using 
      old system compiler.
   MYEXP abbreviates package MyExp 
------------------------------------------------------------------------
   initializing NRLIB MYEXP for MyExp 
   compiling into NRLIB MYEXP 
   compiling exported myexp : (F,PositiveInteger) -> F
Time: 0.10 SEC.
compiling exported myexp : F -> F Time: 0.01 SEC.
(time taken in buildFunctor: 0)
;;; *** |MyExp| REDEFINED
;;; *** |MyExp| REDEFINED Time: 0.01 SEC.
Cumulative Statistics for Constructor MyExp Time: 0.12 seconds
finalizing NRLIB MYEXP Processing MyExp for Browser database: --->-->MyExp((myexp (F F))): Not documented!!!! --->-->MyExp((myexp (F F (PositiveInteger)))): Not documented!!!! --->-->MyExp(constructor): Not documented!!!! --->-->MyExp(): Missing Description ; compiling file "/var/zope2/var/LatexWiki/MYEXP.NRLIB/MYEXP.lsp" (written 30 MAR 2011 07:48:23 PM):
; /var/zope2/var/LatexWiki/MYEXP.NRLIB/MYEXP.fasl written ; compilation finished in 0:00:00.133 ------------------------------------------------------------------------ MyExp is now explicitly exposed in frame initial MyExp will be automatically loaded when needed from /var/zope2/var/LatexWiki/MYEXP.NRLIB/MYEXP
>> System error: The bounding indices 163 and 162 are bad for a sequence of length 162. See also: The ANSI Standard, Glossary entry for "bounding index designator" The ANSI Standard, writeup for Issue SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR

That's generic code and has to be compiled. Look at the types of the result.

axiom
myexp(0.1)

\label{eq1}1.1111111111(1)
Type: Float
axiom
myexp(0.1,4)

\label{eq2}1.1111(2)
Type: Float
axiom
myexp(2,4)

\label{eq3}31(3)
Type: PositiveInteger?
axiom
myexp(2/3,4)

\label{eq4}{211}\over{81}(4)
Type: Fraction(Integer)

And now we create a domain of functions and let it behave like a ring where multiplication is concatenation of functions and addition is... well, see below.

spad
)abbrev domain MYFUN MyFun
MyFun(F: Field): Ring with apply: (%, F) -> F coerce: (F -> F) -> % == add Rep := F -> F X ==> x pretend (F->F) Y ==> y pretend (F->F) zr(f: F): F == 0 id(f: F): F == f plus(x: %, y: %): % == (f: F): F +-> X(f) + Y(f) times(x: %, y: %): % == (f: F): F +-> X(Y f)
0: % == zr 1: % == id (x: %) + (y: %) == plus(x,y) (x: %) * (y: %) == times(x,y) coerce(x: %): OutputForm == "myfun-object" apply(x: %, f: F): F == X f coerce(fun: F -> F): % == fun
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/5099384604506028367-25px003.spad using 
      old system compiler.
   MYFUN abbreviates domain MyFun 
------------------------------------------------------------------------
   initializing NRLIB MYFUN for MyFun 
   compiling into NRLIB MYFUN 
   processing macro definition X ==> pretend(x,F -> F) 
   processing macro definition Y ==> pretend(y,F -> F) 
   compiling local zr : F -> F
Time: 0.08 SEC.
compiling local id : F -> F MYFUN;id is replaced by f Time: 0 SEC.
compiling local plus : ($,$) -> $ Time: 0.01 SEC.
compiling local times : ($,$) -> $ Time: 0 SEC.
compiling exported Zero : () -> $ Time: 0 SEC.
compiling exported One : () -> $ Time: 0 SEC.
compiling exported + : ($,$) -> $ Time: 0 SEC.
compiling exported * : ($,$) -> $ Time: 0.01 SEC.
compiling exported coerce : $ -> OutputForm MYFUN;coerce;$Of;9 is replaced by myfun-object Time: 0 SEC.
compiling exported apply : ($,F) -> F MYFUN;apply;$2F;10 is replaced by SPADCALLfx Time: 0 SEC.
compiling exported coerce : F -> F -> $ MYFUN;coerce;M$;11 is replaced by fun Time: 0.01 SEC.
(time taken in buildFunctor: 10)
;;; *** |MyFun| REDEFINED
;;; *** |MyFun| REDEFINED Time: 0.01 SEC.
Cumulative Statistics for Constructor MyFun Time: 0.12 seconds
finalizing NRLIB MYFUN Processing MyFun for Browser database: --->-->MyFun((apply (F % F))): Not documented!!!! --->-->MyFun((coerce (% (Mapping F F)))): Not documented!!!! --->-->MyFun(constructor): Not documented!!!! --->-->MyFun(): Missing Description ; compiling file "/var/zope2/var/LatexWiki/MYFUN.NRLIB/MYFUN.lsp" (written 30 MAR 2011 07:48:24 PM):
; /var/zope2/var/LatexWiki/MYFUN.NRLIB/MYFUN.fasl written ; compilation finished in 0:00:00.100 ------------------------------------------------------------------------ MyFun is now explicitly exposed in frame initial MyFun will be automatically loaded when needed from /var/zope2/var/LatexWiki/MYFUN.NRLIB/MYFUN
>> System error: The bounding indices 163 and 162 are bad for a sequence of length 162. See also: The ANSI Standard, Glossary entry for "bounding index designator" The ANSI Standard, writeup for Issue SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR

axiom
F := Fraction Integer

\label{eq5}\hbox{\axiomType{Fraction}\ } (\hbox{\axiomType{Integer}\ })(5)
Type: Type
axiom
H := MyFun(F)

\label{eq6}\hbox{\axiomType{MyFun}\ } (\hbox{\axiomType{Fraction}\ } (\hbox{\axiomType{Integer}\ }))(6)
Type: Type
axiom
phi(f: F): F == f/2
Function declaration phi : Fraction(Integer) -> Fraction(Integer) has been added to workspace.
Type: Void
axiom
hphi:H := phi::H
axiom
Compiling function phi with type Fraction(Integer) -> Fraction(
      Integer)

\label{eq7}myfun - object(7)
Type: MyFun?(Fraction(Integer))
axiom
apply(hphi, 1)

\label{eq8}1 \over 2(8)
Type: Fraction(Integer)
axiom
a==>apply
Type: Void

Computing the first 3 terms of 1+\varphi(1)+\varphi(\varphi(1)).

axiom
1+a(hphi,1)+a(hphi,a(hphi,1))

\label{eq9}7 \over 4(9)
Type: Fraction(Integer)

And here we first compute 1+\varphi+\varphi^2 and then apply this function to 1.

axiom
hexp:=myexp(hphi,2)

\label{eq10}myfun - object(10)
Type: MyFun?(Fraction(Integer))
axiom
apply(hexp,1)

\label{eq11}7 \over 4(11)
Type: Fraction(Integer)