|
In Python we might write:
\begin{sageblock}
class Complex:
"Example to demonstrate SPAD equivalent of Python Classes"
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
def real(self):
return self.r
def imag(self):
return self.i
def __str__(self):
if self.i<0:
return "%g - i %g"%(self.real(),-self.imag())
else:
return "%g + i %g"%(self.real(),self.imag())
def __coerce__(self,b):
if isinstance(b,Complex):
return(self,b)
else:
return(self,Complex(b,0))
def __add__(self,b):
return Complex(self.real()+b.real(), self.imag()+b.imag())
def __mul__(self,b):
return Complex(self.real()b.real() - self.imag()b.imag(),
self.real()b.imag() + self.imag()b.real())
\end{sageblock}
Reference
Now we can write:
\begin{sageblock}
x = Complex(3.0, -4.5)
y = Complex(-2.0, 5.4)
\end{sageblock}
\begin{verbatim}
print x+y
\end{verbatim}
\begin{equation}
\label{eq1}
\sage{x+y}
\end{equation}
\begin{verbatim}
print x*y
\end{verbatim}
\begin{equation}
\label{eq2}
\sage{xy}
\end{equation*}
\begin{verbatim}
print x+1.0
\end{verbatim}
\begin{equation}
\label{eq3}
\sage{x+1.0}
\end{equation}
In SPAD (the Axiom first generation extension library compiler)
we would write:
\begin{spad}
)abbrev domain CMPLXS ComplexSpad
+++ Example of SPAD equivalent of Python Class
ComplexSpad(): with
new: (Float,Float) -> % ++ __init__ in Python
coerce: Float -> % ++ __coerce__ in Python
real: % -> Float ++ real in Python
imag: % -> Float ++ imag in Python
_+: (%,%) -> % ++ __add__ in Python
_*: (%,%) -> % ++ __mul__ in Python
coerce: % -> OutputForm ++ __str__ in Python
== add
Rep ==> Record(r:Float,i:Float)
import Rep, OutputForm, Symbol new(realpart:Float,imagpart:Float):% == [realpart,imagpart] pretend %
coerce(x:Float):% == new(x,0)
real(x:%):Float == (x pretend Rep).r
imag(x:%):Float == (x pretend Rep).i
+(x:%, y:%):% ==
new(real(x)+real(y), imag(x)+imag(y))
(x:%, y:%):% ==
new(real(x)real(y) - imag(x)imag(y),
real(x)imag(y) + imag(x)*real(y))
coerce(x:%):OutputForm ==
if imag(x)<0 then
real(x)::OutputForm - outputForm(i) * (-imag(x))::OutputForm
else
real(x)::OutputForm + outputForm(i) * imag(x)::OutputForm
\end{spad}
Reference
\begin{axiom}
x := new(3.0, -4.5)$ComplexSpad
y := new(-2.0, 5.4)$ComplexSpad
x+y
x*y
x+1.0
\end{axiom}
In Aldor we could write:
\begin{aldor}
#pile
#include "axiom.as"
ComplexAldor(): with
new: (Float,Float) -> %
coerce: Float -> %
real: % -> Float
imag: % -> Float
_+: (%,%) -> %
_*: (%,%) -> %
coerce: % -> OutputForm
== add
Rep ==> Record(r:Float,i:Float)
import from Rep, OutputForm, Symbol new(realpart:Float,imagpart:Float):% == per [realpart,imagpart]
coerce(x:Float):% == new(x,0.0)
real(x:%):Float == rep(x).r
imag(x:%):Float == rep(x).i
+(x:%, y:%):% == new(real(x)+real(y), imag(x)+imag(y))
(x:%, y:%):% == new(real(x)real(y) - imag(x)imag(y),
real(x)imag(y) + imag(x)*real(y))
coerce(x:%):OutputForm ==
if imag(x)<0 then
real(x)::OutputForm - outputForm(i) * (-imag(x))::OutputForm
else
real(x)::OutputForm + outputForm(i) * imag(x)::OutputForm
\end{aldor}
Reference
And we get the following result when calling it in Axiom:
\begin{axiom}
x := new(3.0, -4.5)$ComplexAldor
y := new(-2.0, 5.4)$ComplexAldor
x+y
x*y
x+1.0
\end{axiom}
[SandBox SPAD for Python 2]
Some or all expressions may not have rendered properly,
because Axiom returned the following error:
Error: export AXIOM=/usr/local/lib/fricas/target/x86_64-unknown-linux; export ALDORROOT=/usr/local/aldor/linux/1.1.0; export PATH=$ALDORROOT/bin:$PATH; export HOME=/var/zope2/var/LatexWiki; ulimit -t 600; export LD_LIBRARY_PATH=/usr/local/lib/fricas/target/x86_64-unknown-linux/lib; LANG=en_US.UTF-8 $AXIOM/bin/AXIOMsys < /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/4574157217867657312-25px.axm
; file: /var/aw/var/LatexWiki/CMPLXS.NRLIB/CMPLXS.lsp
; in: SDEFUN |CMPLXS;new;2F$;1|
; (BOOT::SDEFUN BOOT::|CMPLXS;new;2F$;1|
; ((BOOT::|realpart| BOOT::|Float|)
; (BOOT::|imagpart| BOOT::|Float|) (BOOT::$ BOOT::$))
; (CONS BOOT::|realpart| BOOT::|imagpart|))
; --> DEFUN PROGN EVAL-WHEN
; ==>
; (SB-IMPL::%DEFUN 'BOOT::|CMPLXS;new;2F$;1|
; (SB-INT:NAMED-LAMBDA BOOT::|CMPLXS;new;2F$;1|
; (BOOT::|realpart| BOOT::|imagpart| BOOT::$)
; (BLOCK BOOT::|CMPLXS;new;2F$;1|
; (CONS BOOT::|realpart| BOOT::|imagpart|)))
; NIL 'NIL (SB-C:SOURCE-LOCATION))
;
; caught STYLE-WARNING:
; The variable $ is defined but never used.
; in: SDEFUN |CMPLXS;real;$F;3|
; (BOOT::SDEFUN BOOT::|CMPLXS;real;$F;3|
; ((BOOT::|x| BOOT::$) (BOOT::$ BOOT::|Float|))
; (BOOT::QCAR BOOT::|x|))
; --> DEFUN PROGN EVAL-WHEN
; ==>
; (SB-IMPL::%DEFUN 'BOOT::|CMPLXS;real;$F;3|
; (SB-INT:NAMED-LAMBDA BOOT::|CMPLXS;real;$F;3|
; (BOOT::|x| BOOT::$)
; (BLOCK BOOT::|CMPLXS;real;$F;3| (BOOT::QCAR BOOT::|x|)))
; NIL 'NIL (SB-C:SOURCE-LOCATION))
;
; caught STYLE-WARNING:
; The variable $ is defined but never used.
; in: SDEFUN |CMPLXS;imag;$F;4|
; (BOOT::SDEFUN BOOT::|CMPLXS;imag;$F;4|
; ((BOOT::|x| BOOT::$) (BOOT::$ BOOT::|Float|))
; (BOOT::QCDR BOOT::|x|))
; --> DEFUN PROGN EVAL-WHEN
; ==>
; (SB-IMPL::%DEFUN 'BOOT::|CMPLXS;imag;$F;4|
; (SB-INT:NAMED-LAMBDA BOOT::|CMPLXS;imag;$F;4|
; (BOOT::|x| BOOT::$)
; (BLOCK BOOT::|CMPLXS;imag;$F;4| (BOOT::QCDR BOOT::|x|)))
; NIL 'NIL (SB-C:SOURCE-LOCATION))
;
; caught STYLE-WARNING:
; The variable $ is defined but never used.
;
; compilation unit finished
; caught 3 STYLE-WARNING conditions
Killed
Checking for foreign routines
AXIOM="/usr/local/lib/fricas/target/x86_64-unknown-linux"
spad-lib="/usr/local/lib/fricas/target/x86_64-unknown-linux/lib/libspad.so"
foreign routines found
openServer result -2
FriCAS Computer Algebra System
Version: FriCAS 1.2.2
Timestamp: Mon Sep 30 18:53:32 UTC 2013
-----------------------------------------------------------------------------
Issue )copyright to view copyright notices.
Issue )summary for a summary of useful system commands.
Issue )quit to leave FriCAS and return to shell.
-----------------------------------------------------------------------------
(1) -> (1) -> (1) -> (1) -> (1) -> <spad>
)abbrev domain CMPLXS ComplexSpad
+++ Example of SPAD equivalent of Python Class
ComplexSpad(): with
new: (Float,Float) -> % ++ __init__ in Python
coerce: Float -> % ++ __coerce__ in Python
real: % -> Float ++ real in Python
imag: % -> Float ++ imag in Python
_+: (%,%) -> % ++ __add__ in Python
_*: (%,%) -> % ++ __mul__ in Python
coerce: % -> OutputForm ++ __str__ in Python
== add
Rep ==> Record(r:Float,i:Float)
import Rep, OutputForm, Symbol new(realpart:Float,imagpart:Float):% == [realpart,imagpart] pretend %
coerce(x:Float):% == new(x,0)
real(x:%):Float == (x pretend Rep).r
imag(x:%):Float == (x pretend Rep).i
+(x:%, y:%):% ==
new(real(x)+real(y), imag(x)+imag(y))
(x:%, y:%):% ==
new(real(x)real(y) - imag(x)imag(y),
real(x)imag(y) + imag(x)*real(y))
coerce(x:%):OutputForm ==
if imag(x)<0 then
real(x)::OutputForm - outputForm(i) * (-imag(x))::OutputForm
else
real(x)::OutputForm + outputForm(i) * imag(x)::OutputForm</spad>
Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/5264130288122953016-25px001.spad
using old system compiler.
CMPLXS abbreviates domain ComplexSpad
------------------------------------------------------------------------
initializing NRLIB CMPLXS for ComplexSpad
compiling into NRLIB CMPLXS
processing macro definition Rep ==> Record(r: Float,i: Float)
importing Record(r: Float,i: Float)
importing OutputForm
importing Symbol
compiling exported new : (Float,Float) -> $
CMPLXS;new;2F$;1 is replaced by CONS
Time: 0 SEC.
compiling exported coerce : Float -> $
Time: 0 SEC.
compiling exported real : $ -> Float
CMPLXS;real;$F;3 is replaced by QCAR
Time: 0 SEC.
compiling exported imag : $ -> Float
CMPLXS;imag;$F;4 is replaced by QCDR
Time: 0 SEC.
compiling exported + : ($,$) -> $
Time: 0 SEC.
compiling exported * : ($,$) -> $
Time: 0 SEC.
compiling exported coerce : $ -> OutputForm
Time: 0 SEC.
(time taken in buildFunctor: 0)
;;; *** |ComplexSpad| REDEFINED
;;; *** |ComplexSpad| REDEFINED
Time: 0 SEC. Cumulative Statistics for Constructor ComplexSpad
Time: 0 seconds
finalizing NRLIB CMPLXS
Processing ComplexSpad for Browser database:
--------constructor---------
--------(new (% (Float) (Float)))---------
--->-->ComplexSpad((new (% (Float) (Float)))): Improper first word in comments:
"__init__ in Python"
--------(coerce (% (Float)))---------
--->-->ComplexSpad((coerce (% (Float)))): Improper first word in comments:
"__coerce__ in Python"
--------(real ((Float) %))---------
--------(imag ((Float) %))---------
--------(+ (% % %))---------
--->-->ComplexSpad((+ (% % %))): Improper first word in comments:
"__add__ in Python"
--------( (% % %))---------
--->-->ComplexSpad(( (% % %))): Improper first word in comments:
"__mul__ in Python"
--------(coerce ((OutputForm) %))---------
--->-->ComplexSpad((coerce ((OutputForm) %))): Improper first word in comments:
"__str__ in Python"
; compiling file "/var/aw/var/LatexWiki/CMPLXS.NRLIB/CMPLXS.lsp" (written 23 FEB 2014 06:23:18 AM):
; /var/aw/var/LatexWiki/CMPLXS.NRLIB/CMPLXS.fasl written
; compilation finished in 0:00:00.039
------------------------------------------------------------------------
ComplexSpad is now explicitly exposed in frame initial
ComplexSpad will be automatically loaded when needed from
/var/aw/var/LatexWiki/CMPLXS.NRLIB/CMPLXS
(1) -> x := new(3.0, -4.5)$ComplexSpad
$$
{3.0} -{i \ {4.5}}
\leqno(1)
$$ Type: ComplexSpad
y := new(-2.0, 5.4)$ComplexSpad
$$
-{2.0}+{i \ {5.4}}
\leqno(2)
$$ Type: ComplexSpad
x+y
$$
{1.0}+{i \ {0.9}}
\leqno(3)
$$ Type: ComplexSpad
x*y
$$
{18.3}+{i \ {25.2}}
\leqno(4)
$$ Type: ComplexSpad
x+1.0
$$
{4.0} -{i \ {4.5}}
\leqno(5)
$$ Type: ComplexSpad
(6) -> <aldor>
#pile
#include "axiom.as"
ComplexAldor(): with
new: (Float,Float) -> %
coerce: Float -> %
real: % -> Float
imag: % -> Float
_+: (%,%) -> %
_*: (%,%) -> %
coerce: % -> OutputForm
== add
Rep ==> Record(r:Float,i:Float)
import from Rep, OutputForm, Symbol
new(realpart:Float,imagpart:Float):% == per [realpart,imagpart]
coerce(x:Float):% == new(x,0.0)
real(x:%):Float == rep(x).r
imag(x:%):Float == rep(x).i
+(x:%, y:%):% == new(real(x)+real(y), imag(x)+imag(y))
(x:%, y:%):% == new(real(x)real(y) - imag(x)imag(y),
real(x)imag(y) + imag(x)*real(y))
coerce(x:%):OutputForm ==
if imag(x)<0 then
real(x)::OutputForm - outputForm(i) * (-imag(x))::OutputForm
else
real(x)::OutputForm + outputForm(i) * imag(x)::OutputForm</aldor>
Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/4438001028188480131-25px003.as
using AXIOM-XL compiler and options
-O -Fasy -Fao -Flsp -laxiom -Mno-ALDOR_W_WillObsolete -DAxiom -Y $AXIOM/algebra -I $AXIOM/algebra
Use the system command )set compiler args to change these
options.
Some or all expressions may not have rendered properly,
because Latex returned the following error:
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
\write18 enabled.
%&-line parsing enabled.
entering extended mode
(./8890045446626885534-16.0px.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, farsi, croatian, ukrainian, russian, bulgarian, czech, slov
ak, danish, dutch, finnish, basque, french, german, ngerman, ibycus, greek, mon
ogreek, ancientgreek, hungarian, italian, latin, mongolian, norsk, icelandic, i
nterlingua, turkish, coptic, romanian, welsh, serbian, slovenian, estonian, esp
eranto, uppersorbian, indonesian, polish, portuguese, spanish, catalan, galicia
n, swedish, ukenglish, pinyin, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size12.clo))
(/usr/share/texmf-texlive/tex/latex/ucs/ucs.sty
(/usr/share/texmf-texlive/tex/latex/ucs/data/uni-global.def))
(/usr/share/texmf-texlive/tex/latex/base/inputenc.sty
(/usr/share/texmf-texlive/tex/latex/ucs/utf8x.def))
(/usr/share/texmf-texlive/tex/latex/bbm/bbm.sty)
(/usr/share/texmf-texlive/tex/latex/jknapltx/mathrsfs.sty)
(/usr/share/texmf-texlive/tex/latex/base/fontenc.sty
(/usr/share/texmf-texlive/tex/latex/base/t1enc.def))
(/usr/share/texmf-texlive/tex/latex/pstricks/pstricks.sty
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.tex
`PSTricks' v1.15 <2006/12/22> (tvz)
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.con))
(/usr/share/texmf/tex/latex/xcolor/xcolor.sty
(/etc/texmf/tex/latex/config/color.cfg)
(/usr/share/texmf-texlive/tex/latex/graphics/dvips.def)
(/usr/share/texmf-texlive/tex/latex/graphics/dvipsnam.def)))
(/usr/share/texmf-texlive/tex/latex/graphics/epsfig.sty
(/usr/share/texmf-texlive/tex/latex/graphics/graphicx.sty
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/latex/graphics/graphics.sty
(/usr/share/texmf-texlive/tex/latex/graphics/trig.sty)
(/etc/texmf/tex/latex/config/graphics.cfg))))
(/usr/share/texmf-texlive/tex/latex/pst-grad/pst-grad.sty
(/usr/share/texmf-texlive/tex/generic/pst-grad/pst-grad.tex
(/usr/share/texmf-texlive/tex/latex/xkeyval/pst-xkey.tex
(/usr/share/texmf-texlive/tex/latex/xkeyval/xkeyval.sty
(/usr/share/texmf-texlive/tex/latex/xkeyval/xkeyval.tex)))
`pst-plot' v1.05, 2006/11/04 (tvz,dg,hv)))
(/usr/share/texmf-texlive/tex/latex/pstricks/pst-plot.sty
(/usr/share/texmf-texlive/tex/generic/pstricks/pst-plot.tex
v97 patch 2, 1999/12/12
(/usr/share/texmf-texlive/tex/generic/multido/multido.tex
v1.41, 2004/05/18 <tvz>)))
(/usr/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/usr/share/texmf-texlive/tex/xelatex/xetexconfig/geometry.cfg)
Package geometry Warning: `lmargin' and `rmargin' result in NEGATIVE (-108.405p
t).
`width' should be shortened in length.
) (/usr/share/texmf-texlive/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `? option.
(/usr/share/texmf-texlive/tex/latex/amsmath/amstext.sty
(/usr/share/texmf-texlive/tex/latex/amsmath/amsgen.sty))
(/usr/share/texmf-texlive/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texmf-texlive/tex/latex/amsmath/amsopn.sty))
(/usr/share/texmf-texlive/tex/latex/amsfonts/amsfonts.sty)
(/usr/share/texmf-texlive/tex/latex/amsfonts/amssymb.sty)
(/usr/share/texmf-texlive/tex/latex/amscls/amsthm.sty)
(/usr/share/texmf-texlive/tex/latex/setspace/setspace.sty
Package: `setspace 6.7 <2000/12/01>
) (/usr/share/texmf-texlive/tex/generic/xypic/xy.sty
(/usr/share/texmf-texlive/tex/generic/xypic/xy.tex Bootstrap'ing: catcodes,
docmode, (/usr/share/texmf-texlive/tex/generic/xypic/xyrecat.tex)
(/usr/share/texmf-texlive/tex/generic/xypic/xyidioms.tex) Xy-pic version 3.7 <1999/02/16>
Copyright (c) 1991-1998 by Kristoffer H. Rose <krisrose@ens-lyon.fr>
Xy-pic is free software: see the User's Guide for details.
Loading kernel: messages; fonts; allocations: state, direction,
utility macros; pictures: \xy, positions, objects, decorations;
kernel objects: directionals, circles, text; options; algorithms: directions,
edges, connections; Xy-pic loaded))
(/usr/share/texmf-texlive/tex/generic/xypic/xyall.tex
Xy-pic option: All features v.3.3
(/usr/share/texmf-texlive/tex/generic/xypic/xycurve.tex
Xy-pic option: Curve and Spline extension v.3.7 curve, circles, loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xyframe.tex
Xy-pic option: Frame and Bracket extension v.3.7 loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xycmtip.tex
Xy-pic option: Computer Modern tip extension v.3.3
(/usr/share/texmf-texlive/tex/generic/xypic/xytips.tex
Xy-pic option: More Tips extension v.3.3 loaded) loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xyline.tex
Xy-pic option: Line styles extension v.3.6 loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xyrotate.tex
Xy-pic option: Rotate and Scale extension v.3.3 loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xycolor.tex
Xy-pic option: Colour extension v.3.3 loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xymatrix.tex
Xy-pic option: Matrix feature v.3.4 loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xyarrow.tex
Xy-pic option: Arrow and Path feature v.3.5 path, \ar, loaded)
(/usr/share/texmf-texlive/tex/generic/xypic/xygraph.tex
Xy-pic option: Graph feature v.3.7 loaded) loaded)
(/usr/share/texmf-texlive/tex/latex/tools/verbatim.sty)
(/usr/share/texmf/tex/latex/graphviz/graphviz.sty
(/usr/share/texmf-texlive/tex/latex/psfrag/psfrag.sty))
(/usr/share/texmf/tex/latex/sagetex.sty
Writing sage input file 8890045446626885534-16.0px.sage
) (/usr/share/texmf-texlive/tex/latex/gnuplottex/gnuplottex.sty
(/usr/share/texmf-texlive/tex/latex/base/latexsym.sty)
(/usr/share/texmf-texlive/tex/latex/moreverb/moreverb.sty)
(/usr/share/texmf-texlive/tex/latex/base/ifthen.sty))
(./8890045446626885534-16.0px.aux)
(/usr/share/texmf-texlive/tex/latex/ucs/ucsencs.def)
(/usr/share/texmf-texlive/tex/latex/base/t1cmtt.fd)
LaTeX Warning: Characters dropped after `\end{sageblock}' on input line 146.
LaTeX Warning: Characters dropped after `\end{sageblock}' on input line 150.
LaTeX Warning: Characters dropped after `\end{verbatim}' on input line 153.
(/usr/share/texmf-texlive/tex/latex/jknapltx/ursfs.fd)
(/usr/share/texmf-texlive/tex/latex/amsfonts/umsa.fd)
(/usr/share/texmf-texlive/tex/latex/amsfonts/umsb.fd)
(/usr/share/texmf-texlive/tex/latex/base/ulasy.fd) [1]
LaTeX Warning: Characters dropped after `\end{verbatim}' on input line 160.
[2]
LaTeX Warning: Characters dropped after `\end{verbatim}' on input line 167.
[3]
Missing $ inserted.
<inserted text>
$
l.180 _
+: (%,%) -> % ++ __add__ in Python
Missing $ inserted.
<inserted text>
$
l.186 Missing $ inserted.
<inserted text>
$
l.191 _
+(x:%, y:%):% ==
Missing $ inserted.
<inserted text>
$
l.201 \end{spad}
\newpage
[4]
LaTeX Warning: Characters dropped after `\end{axiom}' on input line 208.
You can't use `macro parameter character #' in vertical mode.
l.210 #
pile
You can't use `macro parameter character #' in horizontal mode.
l.211 #
include "axiom.as"
Missing $ inserted.
<inserted text>
$
l.217 _
+: (%,%) -> %
Missing $ inserted.
<inserted text>
$
l.223
Missing $ inserted.
<inserted text>
$
l.228 _
+(x:%, y:%):% == new(real(x)+real(y), imag(x)+imag(y))
Missing $ inserted.
<inserted text>
$
l.236 \end{aldor}
\newpage
[5]
LaTeX Warning: Characters dropped after `\end{axiom}' on input line 243.
[6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]
(./8890045446626885534-16.0px.aux) )
(see the transcript file for additional information)
Output written on 8890045446626885534-16.0px.dvi (17 pages, 6272 bytes).
Transcript written on 8890045446626885534-16.0px.log.
|