-
Notifications
You must be signed in to change notification settings - Fork 2
/
mathematicautil.scala
166 lines (143 loc) · 5.01 KB
/
mathematicautil.scala
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package KeYmaeraD
object MathematicaUtil {
import com.wolfram.jlink._
val mathkernel =
{
val mk = System.getenv("MATHKERNEL")
if (mk == null)
{throw new Error("please set the MATHKERNEL variable")}
else mk
}
val linkCall =
"-linkmode launch -linkname '" +
mathkernel +
" -mathlink'"
var mbe_link: Option[KernelLink] = None
val linkLock = new Lock()
val messageBlackList =
List(("Reduce", "nsmet"),
( "FindInstance", "nsmet" ),
("Reduce", "ratnz"))
val mBlist =
new Expr(new Expr(Expr.SYMBOL,"List"),
messageBlackList.map(
x => new Expr(new Expr(Expr.SYMBOL, "MessageName"),
List( new Expr(Expr.SYMBOL, x._1),
new Expr(x._2)).toArray)).toArray)
def createLink: KernelLink = mbe_link match {
case None =>
println("linkCall = " + linkCall)
println("creating mathlink. ")
val link = try {
MathLinkFactory.createKernelLink(linkCall);
} catch {
case e : Throwable =>
println("could not created kernel link")
throw e
}
println("created.")
println("error code = " + link.error())
link.connect
link.discardAnswer
linkLock.synchronized{
mbe_link = Some(link)
}
link
case Some(link) =>
link
}
//--- printing
def math_sym(s: String): Expr =
new Expr(Expr.SYMBOL, s)
def math_int(s: String): Expr =
new Expr(Expr.INTEGER, s)
def un_fun(f: String, arg: Expr): Expr = {
new Expr(math_sym(f),
List(arg).toArray)
}
def bin_fun(f: String, arg1: Expr, arg2: Expr): Expr = {
new Expr(math_sym(f),
List(arg1,arg2).toArray)
}
def mathematica_of_term(tm: Term): Expr = tm match {
case Var(x) => math_sym(x)
case Num(Exact.Integer(n)) => math_int(n.toString)
case Num(Exact.Rational(p,q)) => bin_fun("Divide",
math_int(p.toString),
math_int(q.toString))
case Fn("^",List(tm1,tm2)) =>
bin_fun("Power",
mathematica_of_term(tm1),
mathematica_of_term(tm2))
case Fn("/",List(tm1,tm2)) =>
bin_fun("Divide",
mathematica_of_term(tm1),
mathematica_of_term(tm2))
case Fn("*",List(tm1,tm2)) =>
bin_fun("Times",
mathematica_of_term(tm1),
mathematica_of_term(tm2))
case Fn("-",List(tm1,tm2)) =>
bin_fun("Subtract",
mathematica_of_term(tm1),
mathematica_of_term(tm2))
case Fn("-",List(tm)) =>
un_fun("Minus", mathematica_of_term(tm))
case Fn("+",List(tm1,tm2)) =>
bin_fun("Plus",
mathematica_of_term(tm1),
mathematica_of_term(tm2))
case _ => throw new Error ("don't know how to mathematify " + tm)
}
def math_name(r:String): String = r match {
case "=" => "Equal"
case "/=" => "Unequal"
case "<" => "Less"
case "<=" => "LessEqual"
case ">" => "Greater"
case ">=" => "GreaterEqual"
}
def strip_quant: Formula => (List[String], Formula) = fm => fm match {
case Quantifier(Forall, x, Real, yp@Quantifier(Forall, y, Real, p))=>
val (xs, q) = strip_quant(yp);
(x::xs, q)
case Quantifier(Exists, x, Real, yp@Quantifier(Exists, y, Real, p))=>
val (xs,q) = strip_quant(yp);
(x::xs,q)
case Quantifier(Forall, x, Real, p) => (List(x), p)
case Quantifier(Exists, x, Real, p) => (List(x), p)
case _ => (Nil, fm)
}
def mathematica_of_formula(fm: Formula) : Expr = fm match {
case False => math_sym("False")
case True => math_sym("True")
case Atom(R(r,List(lhs, rhs))) =>
bin_fun(math_name(r),
mathematica_of_term(lhs),
mathematica_of_term(rhs))
case Atom(_) => throw new Error("non-binary relation")
case Not(p) =>
un_fun("Not", mathematica_of_formula(p))
case Binop(And,p,q) =>
bin_fun("And", mathematica_of_formula(p),mathematica_of_formula(q))
case Binop(Or,p,q) =>
bin_fun("Or", mathematica_of_formula(p),mathematica_of_formula(q))
case Binop(Imp,p,q) =>
bin_fun("Implies", mathematica_of_formula(p),
mathematica_of_formula(q))
case Binop(Iff,p,q) =>
bin_fun("Equivalent", mathematica_of_formula(p),
mathematica_of_formula(q))
case Quantifier(Forall, x, Real, p) =>
val (bvs, p1) = strip_quant(fm)
val math_bvs = bvs.map(math_sym)
bin_fun("ForAll", new Expr(math_sym("List"), math_bvs.toArray),
mathematica_of_formula(p1))
case Quantifier(Exists, x, Real, p) =>
val (bvs, p1) = strip_quant(fm)
val math_bvs = bvs.map(math_sym)
bin_fun("Exists", new Expr(math_sym("List"), math_bvs.toArray),
mathematica_of_formula(p1))
case _ => throw new Error ("don't know how to mathematify " + fm)
}
}