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

Edit detail for SandBoxSymbolic revision 33 of 35

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Editor: Bill Page
Time: 2008/07/09 15:24:03 GMT-7
Note: clean up and simplify code for Indeterminant

added:
++   This domain provides basic support for symbols and unevaluated expressions

added:
-- Author: Bill Page

changed:
-      ++ eval(f) passes f to the interpreter.
-    binary   : (%, List %) -> %
-      ++ \spad{binary(op, [a1,...,an])} returns the input form
-      ++ corresponding to  \spad{a1 op a2 op ... op an}.
-    function : (%, List Symbol, Symbol) -> %
-      ++ \spad{function(code, [x1,...,xn], f)} returns the input form
-      ++ corresponding to \spad{f(x1,...,xn) == code}.
-    lambda   : (%, List Symbol) -> %
-      ++ \spad{lambda(code, [x1,...,xn])} returns the input form
-      ++ corresponding to \spad{(x1,...,xn) +-> code} if \spad{n > 1},
-      ++ or to \spad{x1 +-> code} if \spad{n = 1}.
      ++ eval(f) evaluates f with current values of symbols

changed:
-      ++ \spad{a + b} returns the input form corresponding to \spad{a + b}.
      ++ \spad{a + b}

changed:
-      ++ \spad{a - b} returns the input form corresponding to \spad{a - b}.
      ++ \spad{a - b}

changed:
-      ++ \spad{-a} returns the input form corresponding to \spad{-a}.
      ++ \spad{-a}

changed:
-      ++ \spad{a * b} returns the input form corresponding to \spad{a * b}.
      ++ \spad{a * b}

changed:
-      ++ \spad{a / b} returns the input form corresponding to \spad{a / b}.
      ++ \spad{a / b}

changed:
-      ++ \spad{a ** b} returns the input form corresponding to \spad{a ** b}.
      ++ \spad{a ** b}

changed:
-      ++ \spad{a ** b} returns the input form corresponding to \spad{a ** b}.
-    unparse  : % -> String
-      ++ unparse(f) returns a string s such that the parser
-      ++ would transform s to f.
-      ++ Error: if f is not the parsed form of a string.
-    declare  : List %   -> Symbol
-      ++ declare(t) returns a name f such that f has been
-      ++ declared to the interpreter to be of type t, but has
-      ++ not been assigned a value yet.
-      ++ Note: t should be created as \spad{devaluate(T)$Lisp} where T is the
-      ++ actual type of f (this hack is required for the case where
-      ++ T is a mapping type).
-    compile  : (Symbol, List %) -> Symbol
-      ++ \spad{compile(f, [t1,...,tn])} forces the interpreter to compile
-      ++ the function f with signature \spad{(t1,...,tn) -> ?}.
-      ++ returns the symbol f if successful.
-      ++ Error: if f was not defined beforehand in the interpreter,
-      ++ or if the ti's are not valid types, or if the compiler fails.
      ++ \spad{a ** b}

changed:
-    mkProperOp: Symbol -> %
-    strsym    : % -> String
-    tuplify   : List Symbol -> %
    eval x == interpret(x pretend InputForm)

changed:
-
-    conv(ll : List %): % ==
-      convert(ll pretend List SExpression)$SExpression pretend %
-
-    lambda(f,l) == conv([convert("+->"::Symbol),tuplify l,f]$List(%))
-
-    eval x ==
-      v := interpret(x)$Lisp
-      mkObj(unwrap(objVal(v)$Lisp)$Lisp, objMode(v)$Lisp)$Lisp
-
-    convert(x:DoubleFloat):% ==
-      convert(x)$Rep
-
-    strsym s ==
-      string? s => string s
-      symbol? s => string symbol s
-      error "strsym: form is neither a string or symbol"
-
-    unparse x ==
-      atom?(s:% := form2String(x)$Lisp) => strsym s
-      concat [strsym a for a in destruct s]
-
-    declare signature ==
-      declare(name := new()$Symbol, signature)$Lisp
-      name
-
-    compile(name, types) ==
-      symbol car cdr car
-        selectLocalMms(mkProperOp name, convert(name)@%,
-          types, nil$List(%))$Lisp
-
-    mkProperOp name ==
-      op := mkAtree(nme := convert(name)@%)$Lisp
-      transferPropsToNode(nme, op)$Lisp
-      convert op
-
-    binary(op, args) ==
-      (n := #args) < 2 => error "Need at least 2 arguments"
-      n = 2 => convert([op, first args, last args]$List(%))
-      convert([op, first args, binary(op, rest args)]$List(%))
-
-    tuplify l ==
-      empty? rest l => convert first l
-      conv
-        concat(convert("Tuple"::Symbol), [convert x for x in l]$List(%))
-
-    function(f, l, name) ==
-      nn := convert(new(1 + #l, convert(nil()$List(%)))$List(%))@%
-      conv([convert("DEF"::Symbol), conv(cons(convert(name)@%,
-                        [convert(x)@% for x in l])), nn, nn, f]$List(%))
    convert(x:DoubleFloat):% == convert(x)$Rep

changed:
-      conv [convert("+"::Symbol), s1, s2]$List(%)
      convert [convert("+"::Symbol), s1, s2]$List(%)

changed:
-      conv [convert("-"::Symbol), s1, s2]$List(%)
      convert [convert("-"::Symbol), s1, s2]$List(%)

changed:
-      conv [convert("-"::Symbol), s1]$List(%)
      convert [convert("-"::Symbol), s1]$List(%)

changed:
-      conv [convert("*"::Symbol), s1, s2]$List(%)
      convert [convert("*"::Symbol), s1, s2]$List(%)

changed:
-      conv [convert("**"::Symbol), s1, convert n]$List(%)
      convert [convert("**"::Symbol), s1, convert n]$List(%)

changed:
-      conv [convert("/"::Symbol), s1, s2]$List(%)
      convert [convert("/"::Symbol), s1, s2]$List(%)

removed:
-eval(w**2-1)
-eval(w-1)

Symbolic Integers

A simple form of symbolic computation using just Variable and Integer.

axiom
a:Union(Variable a,Integer)
Type: Void
axiom
b:Union(Variable b,Integer)
Type: Void
axiom
c:=a*3-b*2

\label{eq1}-{2 \  b}+{3 \  a}(1)
Type: Polynomial(Integer)
axiom
p:UP(x,Integer):=x*2-7

\label{eq2}{2 \  x}- 7(2)
Type: UnivariatePolynomial?(x,Integer)
axiom
pc := p c

\label{eq3}-{4 \  b}+{6 \  a}- 7(3)
Type: Fraction(Polynomial(Integer))
axiom
f(x)==x^3-x^2+1
Type: Void
axiom
fb := f b
axiom
Compiling function f with type Variable(b) -> Polynomial(Integer)

\label{eq4}{b^3}-{b^2}+ 1(4)
Type: Polynomial(Integer)
axiom
a:=1

\label{eq5}1(5)
Type: Union(Integer,...)
axiom
b:=-3

\label{eq6}- 3(6)
Type: Union(Integer,...)
axiom
c

\label{eq7}-{2 \  b}+{3 \  a}(7)
Type: Polynomial(Integer)
axiom
eval(pc,['a=a,'b=b])

\label{eq8}11(8)
Type: Fraction(Polynomial(Integer))
axiom
eval(c,['a=a,'b=b])

\label{eq9}9(9)
Type: Polynomial(Integer)
axiom
eval(fb,['a=a,'b=b])

\label{eq10}-{35}(10)
Type: Polynomial(Integer)
axiom
a:=3.14  -- not permitted!
Cannot convert right-hand side of assignment 3.14
to an object of the type Union(Variable(a),Integer) of the left-hand side. a:='b -- not permitted!
Cannot convert right-hand side of assignment b
to an object of the type Union(Variable(a),Integer) of the left-hand side.

For more complex cases it is necessary to define a new domain of "indeterminants". These are symbols and unevaluated expressions that can be evaluated at a later time. This domain is modeled after InputForm? which provides all of the basic functionality.

spad
)abbrev domain INDET Indeterminant
++ Description:
++   This domain provides basic support for symbols and unevaluated expressions
++   Based on InputForm
-- Author: Bill Page
Indeterminant():
  Join(SExpressionCategory(String,Symbol,Integer,DoubleFloat,OutputForm),
       ConvertibleTo SExpression) with
    eval: % -> Any
      ++ eval(f) evaluates f with current values of symbols
    "+"      : (%, %) -> %
      ++ \spad{a + b}
    "-"      : (%, %) -> %
      ++ \spad{a - b}
    "-"      : % -> %
      ++ \spad{-a}
    "*"      : (%, %) -> %
      ++ \spad{a * b}
    "/"      : (%, %) -> %
      ++ \spad{a / b}
    "**"     : (%, NonNegativeInteger) -> %
      ++ \spad{a ** b}
    "**"     : (%, Integer) -> %
      ++ \spad{a ** b}
    coerce : Integer -> %
 == SExpression add
    Rep := SExpression
eval x == interpret(x pretend InputForm)
coerce(x:Integer):% == convert(x) coerce(x:%):OutputForm == expr x
convert(x:%):SExpression == x pretend SExpression convert(x:DoubleFloat):% == convert(x)$Rep
s1 + s2 == convert [convert("+"::Symbol), s1, s2]$List(%)
s1 - s2 == convert [convert("-"::Symbol), s1, s2]$List(%)
_-(s1) == convert [convert("-"::Symbol), s1]$List(%)
s1 * s2 == convert [convert("*"::Symbol), s1, s2]$List(%)
s1:% ** n:Integer == convert [convert("**"::Symbol), s1, convert n]$List(%)
s1:% ** n:NonNegativeInteger == s1 ** (n::Integer)
s1 / s2 == convert [convert("/"::Symbol), s1, s2]$List(%)
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/1140494755753515475-25px002.spad using 
      old system compiler.
   INDET abbreviates domain Indeterminant 
------------------------------------------------------------------------
   initializing NRLIB INDET for Indeterminant 
   compiling into NRLIB INDET 
   compiling exported eval : $ -> Any
Time: 0.19 SEC.
compiling exported coerce : Integer -> $ Time: 0.01 SEC.
compiling exported coerce : $ -> OutputForm Time: 0.01 SEC.
compiling exported convert : $ -> SExpression INDET;convert;$Se;4 is replaced by x Time: 0 SEC.
compiling exported convert : DoubleFloat -> $ Time: 0.01 SEC.
compiling exported + : ($,$) -> $ Time: 0.01 SEC.
compiling exported - : ($,$) -> $ Time: 0.01 SEC.
compiling exported - : $ -> $ Time: 0 SEC.
compiling exported * : ($,$) -> $ Time: 0.16 SEC.
compiling exported ** : ($,Integer) -> $ Time: 0 SEC.
compiling exported ** : ($,NonNegativeInteger) -> $ Time: 0.01 SEC.
compiling exported / : ($,$) -> $ Time: 0 SEC.
(time taken in buildFunctor: 0)
;;; *** |Indeterminant| REDEFINED
;;; *** |Indeterminant| REDEFINED Time: 0.02 SEC.
Cumulative Statistics for Constructor Indeterminant Time: 0.43 seconds
finalizing NRLIB INDET Processing Indeterminant for Browser database: --------(eval ((Any) %))--------- --------(+ (% % %))--------- --------(- (% % %))--------- --------(- (% %))--------- --------(* (% % %))--------- --------(/ (% % %))--------- --------(** (% % (NonNegativeInteger)))--------- --------(** (% % (Integer)))--------- --->-->Indeterminant((coerce (% (Integer)))): Not documented!!!! --------constructor--------- ; compiling file "/var/zope2/var/LatexWiki/INDET.NRLIB/INDET.lsp" (written 30 APR 2011 03:08:25 AM):
; /var/zope2/var/LatexWiki/INDET.NRLIB/INDET.fasl written ; compilation finished in 0:00:00.134 ------------------------------------------------------------------------ Indeterminant is now explicitly exposed in frame initial Indeterminant will be automatically loaded when needed from /var/zope2/var/LatexWiki/INDET.NRLIB/INDET
>> 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

Symbolic Matrices

axiom
)clear all
All user variables and function definitions have been cleared.

axiom
m:Union(Indeterminant,Matrix Integer)
Type: Void
axiom
n:Union(Indeterminant,Matrix Integer)
Type: Void
axiom
a:Union(Indeterminant,Integer)
Type: Void
axiom
b:Union(Indeterminant,Integer)
Type: Void
axiom
ab:=(3*a+b)*(a-2*b)

\label{eq11}{\left({3 \  a}+ b \right)}\ {\left(a -{2 \  b}\right)}(11)
Type: Indeterminant
axiom
a:=1$Integer  -- because 0 and 1 can be symbolic!

\label{eq12}1(12)
Type: Union(Integer,...)
axiom
eval ab

\label{eq13}{\left(3 + b \right)}\ {\left(1 -{2 \  b}\right)}(13)
Type: Indeterminant
axiom
b:=0$Integer

\label{eq14}0(14)
Type: Union(Integer,...)
axiom
a:=-10

\label{eq15}-{10}(15)
Type: Union(Integer,...)
axiom
eval ab

\label{eq16}300(16)
Type: PositiveInteger?
axiom
mn1:= m*n-n*m

\label{eq17}{m \  n}-{n \  m}(17)
Type: Indeterminant
axiom
mn2:=(m+n)*(m-n-1)

\label{eq18}{\left(m + n \right)}\ {\left({m - n}- 1 \right)}(18)
Type: Indeterminant
axiom
m:=matrix [[1,2],[3,4]]

\label{eq19}\left[ 
\begin{array}{cc}
1 & 2 
\
3 & 4 
(19)
Type: Union(Matrix(Integer),...)
axiom
n:=matrix [[-1,-2],[-3,4]]

\label{eq20}\left[ 
\begin{array}{cc}
- 1 & - 2 
\
- 3 & 4 
(20)
Type: Union(Matrix(Integer),...)
axiom
eval mn1

\label{eq21}\left[ 
\begin{array}{cc}
0 &{16}
\
-{24}& 0 
(21)
Type: Matrix(Integer)
axiom
eval mn2

\label{eq22}\left[ 
\begin{array}{cc}
0 & 0 
\
{48}& - 8 
(22)
Type: Matrix(Integer)

Questions --yixin.cao, Mon, 07 Jul 2008 10:36:24 -0700 reply
In the Union(Variable a,Integer) way,

axiom
)clear all
All user variables and function definitions have been cleared.

axiom
d: Union(Variable d, Float)
Type: Void
axiom
d*2

\label{eq23}2 \  d(23)
Type: Polynomial(Integer)

will be problematic, and the assignment between symbolic integers is not allowed.

axiom
a: Union(Variable a, Integer)
Type: Void
axiom
b: Union(Variable b, Integer)
Type: Void
axiom
b:=a
a is declared as being in Union(Variable(a),Integer) but has not been given a value.

Re: will be problematic --Bill Page, Mon, 07 Jul 2008 13:48:14 -0700 reply
I agree that the type Union(Variable d,Integer) alone is not sufficient for symbolic computation. But although it might look strange, in fact a result of Polynomial Integer for 2*d is not really a problem. Remember that the use of Integer in Polynomial Integer does not mean that evaluation of the polynomial yields an Integer - it means only that the domain of the coefficients of the polynomial are integers (i.e. that 2 is an integer), it says nothing about 'd':
axiom
p:=2*d

\label{eq24}2 \  d(24)
Type: Polynomial(Integer)
axiom
eval(p,d=3.14)

\label{eq25}6.28(25)
Type: Polynomial(Float)

But there is still a problem since I can write:

axiom
a:Union(Variable a,Integer)
Type: Void
axiom
a:=3.14
Cannot convert right-hand side of assignment 3.14
to an object of the type Union(Variable(a),Integer) of the left-hand side. p:=2*a

\label{eq26}2 \  a(26)
Type: Polynomial(Integer)
axiom
eval(p,a=3.14)

\label{eq27}6.28(27)
Type: Polynomial(Float)

and Axiom does not complain about the eval request because as far as Polynomial Integer is concerned d is just a Symbol.

In the case of the example Indeterminant domain, evaluation of symbolic expressions is always delayed so an appropriate eval operation could be defined that properly respects the type.

Re: assignment between symbolic integers --Bill Page, Mon, 07 Jul 2008 14:00:37 -0700 reply
I am not sure what b:=a should mean. Does it mean that Variable(b) is to be assigned the Symbol a? If so then I think the type should admit this possibility. E.g.
axiom
c:Union(Variable(c),Symbol)
Type: Void
axiom
c:=a

\label{eq28}a(28)
Type: Union(Symbol,...)

the fundamental problem is that, when you define d as Float, the computation of (2 * d) should NOT be polynomial float (nor polynomial integer showed here), but Float, or Symbolic Float and even Expression Float if you want.

PS: In the light of polynomial, (2d) is a non-zero polynomial, so that it's always safe to write (1/(2d)), but it's actually non-safe to do it in the domain of float(or integer).

b:=a is meaningless, of course. But there are many scenarios

axiom
)clear all
All user variables and function definitions have been cleared.

axiom
i:=10
how about i:=-10?

\label{eq29}10(29)
Type: PositiveInteger?
axiom
a: Union(Variable a, Integer)
Type: Void
axiom
b: Union(Variable b, Integer)
Type: Void
axiom
b:= (i>0=>1; a)

\label{eq30}1(30)
Type: Union(Integer,...)

which is meaningful.

Re: Expression Float --Bill Page, Mon, 07 Jul 2008 17:02:43 -0700 reply
Be careful, Expression Float may not be what you think it is. In Axiom Expression R is a domain constructor which extends rational functions with coefficients in R (Fraction Polynomial R) by adding a set of common operators, e.g. sin, sqrt, etc. Again the appearance of Float here does not say anything specific about the result of evaluating the expression or even the values that can be associated with it's generators. I do not know why one might prefer Expression Float over Polynomial Float or even Polynomial Integer.

We wish to place a restriction on the possible values that certain symbols can take. Such symbols and expressions formed from them certainly cannot live in a numeric domain such as Float. Except in certain circumstances (mentioned previously) I think a domain such as a:Union(Variable(a),Float) and the domains to which it can be coerced (such as Polynomial, Expression, ...) does already represent such a symbolic expressions fairly well.

But 1) What is Symbolic Float? Can you give a description?

And 2) How can I express the concept of "non-zero polynomial" in Axiom? I might use a domain that does not include zero for example a:Union(Variable(a),PositiveInteger) but there is no general domain of NonZero anything.

Well, I don't like Expression Float. But I notice somebody takes it as a choice, so I put it there with "if you want".

Ok, back to my own point. what's the justification of that the multiplication of two floats returns a polynomial float? Even the value is unknown, the type is certain, and float is closed under multiplication, so the return must be still a float, right?

AFAIK, Axiom currently support only polynomials with coefficients of known values. Then the only zero-polynomial(Integer) is 0$Polynomial Integer, others are non-zero polynomials.

Re: justification --Bill Page, Tue, 08 Jul 2008 22:22:15 -0700 reply
The multiplication of a Float and some unknown symbolic value must produce a symbolic expression of some kind. If we know that the currently unknown symbolic value can only take values from Float, then we can deduce from knowledge of multiplication in Float that the value of the symbolic expression representing the multiplication of a Float with this unknown symbolic value must also only take values from Float.

But Axiom currently does not have any domain whose values are symbolic expressions which only evaluate to values in some specific domain. Polynomial is one of the existing domains in Axiom whose values are symbolic expressions (of a very specific type). Symbolic expression in the domain Expression are more general but still rather restricted in form. Finally there is InputForm? which consists of fully unevaluated expressions of the most general form allowed in Axiom.

Perhaps what you have called "Symbolic Float" could be implemented as an extension of the domain I called "Indeterminant" above.

Re: nonzero polynomials --Bill Page, Tue, 08 Jul 2008 22:35:03 -0700 reply
yixin.cao wrote:
the only zero-polynomial(Integer) is 0$Polynomial Integer, others are non-zero polynomials.

But a polynomial containing at least one monomial term is symbolic. Certain combinations of values substituted for the symbols may result in zero (such values are called is "solution").

yixin.cao wrote:
In the light of polynomial, (2d) is a non-zero polynomial, so that it's always safe to write (1/(2d))

If a value of 0 is subsituted for d in this "non-zero polynomial" then in what sense is 1/(2d) safe?

Re: nonzero polynomials --yixin.cao, Wed, 09 Jul 2008 07:19:05 -0700 reply
For the first question: I don't understand your point on the "one monomial term", polynomial is always symbolic (with the exception of constants). Even in the example of (d: Union(Variable d, Integer), d*2), if you treat (2d) together as the coefficient and the whole as a constant polynomial, then you already admit that (2d) is an integer, haaa..

For the second question: Yes, polynomial (x-1) might be evaluated to 0 (all of Integer,Float,Complex?), but this doesn't change the matter of (x-1) is not a zero-polynomial. Well, do you think (x**2-1)/(x-1) = (x+1) is legal and guaranteed in the domain of polynomial? But it's not in the domain of Float/Integer. You don't want to modify the rule of polynomial to accommodate this, right?

The polynomial 0, which may be considered to have no terms at all, is called the zero polynomial. (from http://en.wikipedia.org/wiki/Polynomial)

Re: justification --yixin.cao, Wed, 09 Jul 2008 07:32:17 -0700 reply
"But Axiom currently does not have any domain whose values are symbolic expressions which only evaluate to values in some specific domain" This statement is not the justification of picking one domain that is closest to the requirement and using it, but a justification of making one.

Domain "Indeterminant" is a good example for this, thanks.

Re: nonzero polynomials --Bill Page, Wed, 09 Jul 2008 14:19:02 -0700 reply
Yes,
axiom
x:Polynomial Float
Type: Void
axiom
p:=(x**2-1)/(x-1)

\label{eq31}x +{1.0}(31)
Type: Fraction(Polynomial(Float))
axiom
eval(p,x=1)

\label{eq32}2.0(32)
Type: Fraction(Polynomial(Float))

is certainly legal and safe in the domain of polynomial where the result is required to be a polynomial although it seems that Axiom is being careful to give the result type as Fraction Polynomial Float and not automatically Polynomial Float.

Do you agree that it is not safe if all we know about x is that it is a symbol which can be replaced with a Float?

In this case a domain like Indeterminant can deal with it.

axiom
)library INDET
Indeterminant is already explicitly exposed in frame initial Indeterminant will be automatically loaded when needed from /var/zope2/var/LatexWiki/INDET.NRLIB/INDET w:Union(Integer,Indeterminant)
Type: Void
axiom
q:=(w**2-1)/(w-1)

\label{eq33}{{<em> </em> \left({w , \: 2}\right)}- 1}\over{w - 1}(33)
Type: Indeterminant
axiom
w:=1

\label{eq34}1(34)
Type: Union(Integer,...)
axiom
eval(q)

\label{eq35}{{<em> </em> \left({1, \: 2}\right)}- 1}\over 0(35)
Type: Indeterminant

Reasonable result --yixin.cao, Wed, 09 Jul 2008 14:42:06 -0700 reply
This is the reasonable result most users can expect.