symbolic mathematics in algorithms - algorithm

I am going throgh algorithms. It is mentioned that one of the application of algorithms is symbolic mathematics. And I found following defintion from dictionary as below.
The use of computers to manipulate mathematical equations and
expressions in symbolic form, as opposed to manipulating the numerical
quantities represented by those symbols. Such a system might be used
for symbolic integration or differentiation, substitution of one
expression into another, simplification of an expression, change of
subject etc. One of the best known symbolic mathematics software
packages is Mathematica.
My question what does statement "equations and expressions in symbolic form, as opposed to manipulating the numerical quantities represented by those symbols." mean?
Thanks!

My question what does statement "equations and expressions in symbolic
form, as opposed to manipulating the numerical quantities represented
by those symbols." mean?
By the second, something like this is meant:
>>> x = 2.3
>>> y = 9.8
>>> z = x+2*y
>>> z
21.900000000000002
>>> type(z)
<type 'float'>
where you're treating x, y, and z as names for numbers. You're using the computer as an old-fashioned calculator, where it only does arithmetic. z = x+2*y performs the arithmetic operations described on the right and associates the resulting number to z.
By the "symbolic form", something more like this is meant:
sage: x, y, z = var("x y z")
sage: z == x+2*y
z == x + 2*y
sage: eq = z == x+2*y
sage: eq
z == x + 2*y
sage: type(z)
<type 'sage.symbolic.expression.Expression'>
sage: parent(eq)
Symbolic Ring
sage: eq.solve(y)
[y == -1/2*x + 1/2*z]
where x,y, and z can be expressions, or variables in some structure, rather than merely names for specific numbers, and higher-level operations can be performed.

Related

Combine boolean and integer logic in linear arithmetic using the Z3 Solver?

I would like to solve problems combining boolean and integer logic in linear arithmetic with a SAT/SMT solver. At first glance, Z3 seems promising.
First of all, is it at all possible to solve the following problem? This answer makes it seem like it works.
int x,y,z
boolean a,b,c
( (3x + y - 2z >= 10) OR (A AND (NOT B OR C)) OR ((A == C) AND (x + y >= 5)) )
If so, how does Z3 solve this kind of problem in theory and is there any documentation about it?
I could think of two ways to solve this problem. One would be to convert the Boolean operations into a linear integer expression. Another solution I read about is to use the Nelson-Oppen Combination Method described in [Kro 08].
I found a corresponding documentation in chapter 3.2.2. Solving Arithmetical Fragments, Table 1 a listing of the implemented algorithms for a certain logic.
Yes, SMT solvers are quite good at solving problems of this sort. Your problem can be expressed using z3's Python interface like this:
from z3 import *
x, y, z = Ints('x y z')
A, B, C = Bools('A B C')
solve (Or(3*x + y - 2*z >= 10
, And(A, Or(Not(B), C))
, And(A == C, x + y >= 5)))
This prints:
[A = True, z = 3, y = 0, B = True, C = True, x = 5]
giving you a (not necessarily "the") model that satisfies your constraints.
SMT solvers can deal with integers, machine words (i.e., bit-vectors), reals, along with many other data types, and there are efficient procedures for combinations of linear-integer-arithmetic, booleans, uninterpreted-functions, bit-vectors amongst many others.
See http://smtlib.cs.uiowa.edu for many resources on SMT solving, including references to other work. Any given solver (i.e., z3, yices, cvc etc.) will be a collection of various algorithms, heuristics and tactics. It's hard to compare them directly as each shine in their own way for certain sublogics, but for the base set of linear-integer arithmetic, booleans, and bit-vectors, they should all perform fairly well. Looks like you already found some good references, so you can do further reading as necessary; though for most end users it's neither necessary nor that important to know how an SMT solver internally works.

How to use Wolfram Alpha to find a set of solutions that hold for any variables in the domain

Say that x and y are real numbers and y > 0. And say that I want to find for which values of A do (A + x + y > 0) and (A + x - y > 0) always hold, as long as x, y are in the domain.
How would I specify that on Wolfram Alpha? (Note: obviously these equations have no solution, but I just used it as an example.)
Or, if not on Wolfram, what software/website could I use?
I tried to write: solve for A: [input my first equation], y>0
but that didn't work, as it only gave integer solutions for when A, x, and y vary, instead of finding values of A such that it always holds no matter what x, y are.
https://www.wolframalpha.com/input?i=%28A+%2B+x+%2B+y+%3E+0%29+and+%28A+%2B+x+-+y+%3E+0%29+
[x>-A, -A - x<y<A + x]

How to get result from z3py calculation?

I want to use z3py to illustrate the following genealogy exercise (pa is “parent” and grpa is “grand-parent)
pa(Rob,Kev) ∧ pa(Rob,Sama) ∧ pa(Sama,Tho) ∧ pa(Dor,Jim) ∧ pa(Bor,Jim) ∧ pa(Bor,Eli) ∧ pa(Jim,Tho) ∧ pa(Sama,Samu) ∧ pa(Jim,Samu) ∧ pa(Zel,Max) ∧ pa(Samu,Max)
∀X,Y,Z pa(X,Z) ∧ pa(Z,Y) → grpa(X,Y)
The exercise consists in finding for which value of X one has the following:
∃X grpa(Rob,X) ∧ pa(X,Max)
(The answer being: for X == Samu.) I would like to rewrite this problem in z3py, so I introduce a new sort Hum (for “humans”) and write the following:
import z3
Hum = z3.DeclareSort('Hum')
pa = z3.Function('pa',Hum,Hum,z3.BoolSort())
grpa = z3.Function('grpa',Hum,Hum,z3.BoolSort())
Rob,Kev,Sama,Tho,Dor,Jim,Bor,Eli,Samu,Zel,Max = z3.Consts('Rob Kev Sama Tho Dor Jim Bor Eli Samu Zel Max', Hum)
s=z3.Solver()
for i,j in ((Rob,Kev),(Rob,Sama),(Sama,Tho),(Dor,Jim),(Bor,Jim),(Bor,Eli),(Jim,Tho),(Sama,Samu),(Jim,Samu),(Zel,Max),(Samu,Max)):
s.add(pa(i,j))
x,y,z=z3.Consts('x y z',Hum)
whi=z3.Const('whi',Hum)
s.add(z3.ForAll([x,y,z],z3.Implies(z3.And(pa(x,z),pa(z,y)),grpa(x,y))))
s.add(z3.Exists(whi,z3.And(grpa(Rob,whi),pa(whi,Max))))
The code is accepted by Python and for
print(s.check())
I get
sat
Now I know there is a solution. The problem is: how do I get the value of whi?
When I ask for print(s.model()[whi]) I get None. When I ask for s.model().evaluate(whi) I get whi, which is not very helpful.
How can I get the information that whi must be Samu for the last formula to be true?
(Auxiliary question: why is there no difference between constants and variables? I'm a bit puzzled when I define x,y,z as constants although they are variable.
Why can I not write x=Hum('x') to show that x is a variable of sort Hum?)
When you write something like:
X, Y = Const('X Y', Hum)
It does not mean that you are declaring two constants named X and Y of sort Hum. (Yes, this is indeed confusing! Especially if you're coming from a Prolog like background!)
Instead, all it means is that you are saying there are two objects X and Y, which belong to the sort Hum. It does not even mean X and Y are different. They might very well be the same, unless you explicitly state it, like this:
s.assert(z3.Distinct([X, Y]))
This might also explain your confusion regarding constants and variables. In your model, everything is a variable; you haven't declared any constants at all.
Your question about how come whi is not Samu is a little trickier to explain, but it stems from the fact that all you have are variables and no constants at all. Furthermore, whi when used as a quantified variable will never have a value in the model: If you want a value for a variable, it has to be a top-level declared variable with its own assertions. This usually trips people who are new to z3py: When you do quantification over a variable, the top-level declaration is a mere trick just to get a name in the scope, it does not actually relate to the quantified variable. If you find this to be confusing, you're not alone: It's a "hack" that perhaps ended up being more confusing than helpful to newcomers. If you're interested, this is explained in detail here: https://theory.stanford.edu/~nikolaj/programmingz3.html#sec-quantifiers-and-lambda-binding But I'd recommend just taking it on faith that the bound variable whi and what you declared at the top level as whi are just two different variables. Once you get more familiar with how z3py works, you can look into the details and reasons behind this hack.
Coming back to your modeling question: You really want these constants to be present in your model. In particular, you want to say these are the humans in my universe and nobody else, and they are all distinct. (Kind of like Prolog's closed world assumption.) This sort of thing is done with a so-called enumeration sort in z3py. Here's how I would go about modeling your problem:
from z3 import *
# Declare an enumerated sort. In this declaration we create 'Human' to be a sort with
# only the elements as we list them below. They are guaranteed to be distinct, and further
# any element of this sort is guaranteed to be equal to one of these.
Human, (Rob, Kev, Sama, Tho, Dor, Jim, Bor, Eli, Samu, Zel, Max) \
= EnumSort('Human', ('Rob', 'Kev', 'Sama', 'Tho', 'Dor', 'Jim', 'Bor', 'Eli', 'Samu', 'Zel', 'Max'))
# Uninterpreted functions for parent/grandParent relationship.
parent = Function('parent', Human, Human, BoolSort())
grandParent = Function('grandParent', Human, Human, BoolSort())
s = Solver()
# An axiom about the parent and grandParent functions. Note that the variables
# x, y, and z are merely for the quantification reasons. They don't "live" in the
# same space when you see them at the top level or within a ForAll/Exists call.
x, y, z = Consts('x y z', Human)
s.add(ForAll([x, y, z], Implies(And(parent(x, z), parent(z, y)), grandParent(x, y))))
# Express known parenting facts. Note that unlike Prolog, we have to tell z3 that
# these are the only pairs of "parent"s available.
parents = [ (Rob, Kev), (Rob, Sama), (Sama, Tho), (Dor, Jim) \
, (Bor, Jim), (Bor, Eli), (Jim, Tho), (Sama, Samu) \
, (Jim, Samu), (Zel, Max), (Samu, Max) \
]
s.add(ForAll([x, y], Implies(parent(x, y), Or([And(x==i, y == j) for (i, j) in parents]))))
# Find what makes Rob-Max belong to the grandParent relationship:
witness = Const('witness', Human)
s.add(grandParent(Rob, Max))
s.add(grandParent(Rob, witness))
s.add(parent(witness, Max))
# Let's see what witness we have:
print s.check()
m = s.model()
print m[witness]
For this, z3 says:
sat
Samu
which I believe is what you were trying to achieve.
Note that the Horn-logic of z3 can express such problems in a nicer way. For that see here: https://rise4fun.com/Z3/tutorialcontent/fixedpoints. It's an extension that z3 supports which isn't available in SMT solvers, making it more suitable for relational programming tasks.
Having said that, while it is indeed possible to express these sorts of relationships using an SMT solver, such problems are really not what SMT solvers are designed for. They are much more suitable for quantifier-free fragments of logics that involve arithmetic, bit-vectors, arrays, uninterpreted-functions, floating-point numbers, etc. It's always fun to try these sorts of problems as a learning exercise, but if this sort of problem is what you really care about, you should really stick to Prolog and its variants which are much more suited for this kind of modeling.

How to rearrange a function y = f[x] into x = g[y]

I have a differential equation A*dx/dt + B(y-y0) = 0
Where x is a very complicated function of y.
How can I use Mathematica to rearrange y to get a function x in order to solve this?
Thanks
There are two or three different problems here that you might be asking:
Option 1: The subject line
First, if you really do have a function f[x] defined and you want to rearrange it, you would be doing something like this:
f[x_]=2+x+x^2;
Solve[y==f[x],x]
However, even here you should notice that inverse functions are not necessarily unique. There are two functions given, and the domain of each is only for y>=7/4.
Option 2: Solving a DE
Now, the equation you give is a differential equation. That is not the same as "rearranging a function y=f[x] into x=g[y]" because there are derivatives involved.
Mathematica has a built-in differential-equation solver:
DSolve[a y'[t] + b (y[t] - y0) == 0, y[t], t]
That will give you a function (in terms of constants $a,b,y_0$) that is the answer, and it will include the unspecified constant of integration.
Your system seems to refer to two functions, x(t) and y(t). You cannot solve one equation for two variables, so it is impossible to solve this (Mathematica or otherwise) without more information.
Option 3: Rearranging an expression
As a third alternative, if you are trying to rearrange this equation without solving the differential equation, you can do that:
Solve[a x'[t] + b(y[t]-y0)==0,x'[t]]
This will give you $x'(t)$ in terms of the other constants and the function $y(t)$, but in order to integrate this (i.e. to solve the differential equation) you will need to know more about y[t].

Evaluating three-variable expression in Prolog

Follow the Four-Step Abstract design process to define recursive rules to compute mathematical functions. You must indicate (use comments to code) which step is used. Note, a Prolog rule does not return a value. You need to use a parameter to hold the return value. You may NOT use the exponential operator ** to compute the expressions.
Write a recursive rules factbar(F, X, Y, N) to compute F = ((2*X + Y)^N)! (factorial of expbar). The rule must call (use) the rule expbar that you designed..
Now for doing this operation F = ((2*X + Y)^N) I have already written my code but I do not know how to write factorial in Prolog:
expbar(R, X, Y, N) :-
X > 0, Y > 0, N > 0,
R is (2 * X + Y) ** N.
Although I have used ** in my program for exponent I did not know how to use the other way.
I have no idea what the "four step abstract design process" is and you haven't included that detail. As a result, you're going to instead get my two-step recursive function design process. Your predicate is right except you haven't defined pow/3, a function to compute powers. This is obviously the crux of your assignment. Let's do it.
Step one: identify your base cases. With arithmetic functions, the base case involves the arithmetic identity. For exponentiation, the identity is 1. In other words, X**1 = X. Write this down:
pow(X,1,X).
Because this is a function with two inputs and one result, we'll encode it as an arity-3 predicate. This fact simply says X to the 1st power is X.
Step two. Now consider the inductive case. If I have X**N, I can expand it to X * (X**(N-1)). By the definition of exponentiation and the induction rule, this completes the definition of the predicate. Encode it in Prolog syntax:
pow(X,N,Y) :-
N > 1,
succ(N0, N),
pow(X, N0, Y0),
Y is X * Y0, !.
This gives you a predicate for calculating exponents. If you replace your use of **/2 in your expbar/4 predicate, you fulfill the requirements of your assignment.

Resources