How can I transform a mathematical expression into an easier-to-parse way, in Julia? - parsing

Is there a predefined class or package in Julia that can handle this sort of transformation: x * (y - z) => [y, z, "-"; x, y, "*"] (this assumes that in the interest of saving memory space, the output of the first operation is stored in variable y before the second operation is applied).
Basically, I need to break down a mathematical expression into a series of steps which I can then apply to the variables involved, algorithmically.
Thanks!

You can get an abstract syntax tree from an expression.
Since it is a Julian data structure you can than process or visualize it any way you want.
julia> dump(:(x * (y - z)))
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol *
2: Symbol x
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol -
2: Symbol y
3: Symbol z
This mechanism is described in the Metaprogramming section of Julia manual
Depending on what you actually need to do this question might be also related: Julia What is The Fastest Way to Evaluate a Math Tree

Related

Take out common variables using Z3

I have a formlua in DNF form, say:
abcx + abcy + abz
Is there any way to take out the common variables, to get the follwing formula:
ab (cx + cy + z)
A followup question, can it be done recursively, like
ab ( c(x+y) + z)
Sure.. Here's one way:
from z3 import *
a, b, c, x, y, z = Ints('a b c x y z')
print simplify(a*b*c*x + a*b*c*y + a*b*z, hoist_mul=True)
This prints:
a*b*(c*(x + y) + z)
which is exactly what you're looking for.
And for your next question, how did I find about hoist_cmul=True argument? Simply run:
help_simplify()
at your Python prompt, and it'll list you all the options simplify takes.
Note that you should in general not count on what the simplifier will give you. It's mostly heuristic driven, and in the presence of other terms what you get may not match what you expected. (It'll of course still be an equivalent expression.) There's no notion of "simplest" when it comes to arithmetic expressions, and what you consider simple and what z3 considers simple may not necessarily match.

maxima gradef differentiation

I try to define the derivative of the standard normal pdf in terms of the function:
φ(x) := exp (-x^2/2)/sqrt(2 * %pi);
gradef(φ(x),-x*φ(x));
but if I type then:
diff(φ(x),x);
I get:
-(x*%e^(-x^2/2))/(sqrt(2)*sqrt(%pi))`
not as I want -x*φ(x).
What I am doing wrong?
Thanks do not
Karl
EDiT :
Unfortunately both suggestions do not work.
I think there's nothing wrong; Maxima is just evaluating phi according to the definition you gave when you call gradef.
I can think of a couple of things to try. (1) Call gradef before defining phi. Then maybe you'll get phi in the output when you call diff. Not sure if that will work.
(2) Define the gradef using a noun expression, i.e., gradef(φ(x),-x*'φ(x)). Notice the single quote mark ' before φ(x); that makes a so-called noun expression in which the argument x may be evaluated but the function φ is not called. Later on in order to evaluate the function, when you want to, you can say ev(someexpression, nouns) to evaluate all noun expressions in someexpression.
EDIT: Here's another idea. This works for me. The previous ideas didn't work because φ gets evaluated too soon; this new idea goes to a greater length to prevent evaluation. Note that the gradef is defined for 'φ(x), so you have to write diff('φ(x), x) in order to apply the gradef.
(%i12) gradef('φ(x), -x*'φ(x));
(%o12) φ(x)
(%i13) diff('φ(x), x);
(%o13) - x φ(x)
The gradef produces a noun expression -x*'φ(x), so to verbify it you can say:
(%i14) ev(%, nouns);
2
x
- --
2
x %e
(%o14) - -----------------
sqrt(2) sqrt(%pi)
Looks like the chain rule is applied as expected:
(%i15) diff('φ(x/a), x);
x
x φ(-)
a
(%o15) - ------
2
a
(%i16) ev(%, nouns);
2
x
- ----
2
2 a
x %e
(%o16) - --------------------
2
sqrt(2) sqrt(%pi) a

What is a "Production" in plain English?

I can read on Wikipedia the formal definition of a Production, however when you start reading that article, it makes an assumption about prior knowledge.
Wikipedia defines it as follows:
A production or production rule in computer science is a rewrite rule specifying a symbol substitution that can be recursively performed to generate new symbol sequences.
This assumes that I know and understand what a rewrite rule is. I don't, and if I click the link, I get into another fairly technical explanation.
Can someone explain to me in plain English what a Production actually is?
Note: I have made many attempts to understand this, but I don't think I've succeeded. From what I can tell it rewrites the given string in terms of grammar rules. Not sure if I'm correct.
To explain what a production is I'd like to introduce a bit of context first.
The dragon book states that a context free grammar has 4 components:
a set of terminal symbols (tokens)
a set of non-terminal symbols (syntactic variables)
a set of productions of the form: non-term --> sequence of terminals and non-terminals
a non-terminal symbol designated as the start symbol
It is also said that parsing is the problem of taking a string of terminals (the source code) and figuring out what are the steps required to derive this string of terminals from the start symbol of the grammar.
Now that this has been said, a production is essentially a possible (intermediate) step. I say possible because some symbols can derive into different sequences.
For example, let's make a simple grammar to represent an arbitrarily long sequences of a's ending with a b. The 4 components of this grammar would be:
Terminals: a, b
Non-terminals: S, X
Rules: S --> X, X --> aX, X --> ab
Start symbol: S
From the description I gave above "aaaab" should be derivable from this grammar. Let's see if that holds up. We start from, the start symbol, and then apply productions until a) we get the final sequence, b) we exhaust all possibilities without succeeding (meaning the sequence is not "grammatically correct").
S
X (after applying S --> X)
aX (after applying X --> aX)
aaX (after applying X --> aX)
aaaX (after applying X --> aX)
aaaab (after applying X --> ab)
And we're done, we got the original sequence. So as you can see we re-wrote the non-terminal symbols by applying rules (one of them we applied recursively) which transformed the sequence into a new sequence of symbols at every step and we did so until we had the final sequence.
A rewrite rule is a method of replacing subterms of a formula with other terms. In their most basic form, they consist of a set of objects, plus relations on how to transform those objects.
An example of a rewrite rule could look like:
A → B
Now as for what this actually does! You are right on your note, take for example a list of things (and 2 rewrite rules):
X, Y, Z
X → Y
Y → Z
Which would result in:
Z, Z, Z
A production rule is a rewrite rule because it is a method of replacing subterms of a formula (probably a string in your case). A production rule could look like:
X, Y, Z
X → aX
By using the rule in such a way it becomes possible to apply recursion (create new sequences) as it will keep replacing itself:
aX, Y, Z
aaX, Y, Z
aaaX, Y, Z
As for the question you are asking, you could say: "A production rule is a replacement rule for formulas that uses recursion to create new sequences".

Binary trees, construct a tree based on preorder

constructing a tree given it's inorder is easy enough.
But, let's say you are supposed to construct a tree based on it's preorder (+ + y z + * x y z for example).
It's easy to see that + is the root, and how to continue in the left subtree from there.
But.. how do you know when you are supposed to "switch" to the right subtree?
Usually, inorder is considered the difficult case.
For preorder, you'll just have a grammar like this.
expr ::= operator expr expr | var
An operator is followed by exactly two well-formed expressions. This can be parsed easily using recursion
If you parse a tree and get a variable, return the variable.
If you parse a tree and get an operator, parse the two following trees as right/left subtrees.

refactoring boolean equation

Let's say you have a Boolean rule/expression like so
(A OR B) AND (D OR E) AND F
You want to convert it into as many AND only expressions as possible, like so
A AND D AND F
A AND E AND F
B AND D AND F
B AND E AND F
You are just reducing the OR's so it becomes
(A AND D AND F) OR (A AND E AND F) OR (...)
Is there a property in Boolean algebra that would do this?
Take a look at DeMorgan's theorem. The link points to a document relating to electronic gates, but the theory remains the same.
It says that any logical binary expression remains unchanged if we
Change all variables to their complements.
Change all AND operations to ORs.
Change all OR operations to ANDs.
Take the complement of the entire expression.
(quoting from the above linked document)
Your example is exploiting the the distributivity of AND over OR, as shown here.
All you need to do is apply that successively. For example, using x*(y+z) = (x*y)+(x*z) (where * denotes AND and + denotes OR):
0. (A + B) * (D + E) * F
1. Apply to the first 2 brackets results in ((A+B)*D)+((A+B)*E)
2. Apply to content of each bracket results in (A*D+B*D)+(A*E+B*E)
3. So now you have ((A*D+B*D)+(A*E+B*E))*F
4. Applying the law again results in (A*D+B*D)*F+(A*E+B*E)*F
5. Apply one more time results in A*D*F+B*D*F+A*E*F+B*E*F, QED
You may be interested in reading about Karnaugh maps. They are a tool for simplifying boolean expressions, but you could use them to determine all of the individual expressions as well. I'm not sure how you might generalize this into an algorithm you could write a program for though.
You might be interested in Conjunctive Normal form or its brother, Disjunctive normal form.
As far as I know boolean algebra can not be build only with AND and OR operations.
If you have only this two operation you are not able to receive NOT operation.
You can convert any expression to the full set of boolean operations.
Here is some full sets:
AND and NOT
OR and NOT
Assuming you can use the NOT operation, you can rewrite any Boolean expression with only ANDs or only ORs. In your case:
(A OR B) AND (D OR E) AND F
I tend to use engineering shorthand for the above and write:
AND as a product (. or nothing);
OR as a sum (+); and
NOT as a single quote (').
So:
(A+B)(D+E)F
The corollary to arithmetic is actually quite useful for factoring terms.
By De Morgan's Law:
(A+B) => (A'B')'
So you can rewrite your expression as:
(A+B)(D+E)F
(A'B')'(D'E')'F

Resources