Need help on BDDs graphs - boolean-expression

I am reimplementing (for fun, a bit of work and a severe case of NIH) a ROBDD library. I would like to have a few "reference" graphs built by other libraries to compare results [*]
E.g., given the variable order x1 < y1 < x2 < y2, what would be the graph obtained for
(x1 <=> y1) /\ not (x2 <=> y2) /\ (x2 <=> y2) [**]
I am assuming standard operators. Also, if it helps, I assume /\ is left-associative.
Any other smallish example will be welcome.
Thanks
Marco
[*] I know! I should download libraries, install them and use them, but I am lazy.
[**] The example is taken from a the Moeller and Oestergard's example that is floating around the net.

I am the author of BDD Scout - a tool for visualisation of BDDs. It works on MS Windows and GNU/Linux. It can show you a ROBDD with complemented edges for any Boolean function you entered. It supports also ROBDDs without complemented edges and 0-sup-BDDs (aka ZDDs introduced by Minato). It also allows you to reorder variables. You can download it from http://biddy.meolic.com/ . Your formula is equal to zero but, for example, below is a graph produced for Boolean function:
F = (x1*y1+!x1*!y1) * !(x2*y2+!x2*!y2)
EDIT: Please find below two PNG files generated with BDD Scout for Boolean function from Moeller and Oestergard's Bachelor's Thesis (page 9). Unfortunately, BDD Scout does not support <=> and thus the function must be specified as:
F = !( (!(x1^y1) * (x2^y2)) ^ !(x2^y2) )

Related

How to define custom functions in Maple?

I'm new to Maple and I'm looking for a simple way to automate some tasks. In particular, I'm looking for a way to define custom "action" that perform some steps automatically.
As as an example I would like to define a quick way to compute the determinant of the Hessian of a polynomial. Currently the way I do this is opening Maple, create a new worksheet than performing the following commands:
p := (x, y) -> x^2*y + 3*x^3 + y^3
with(VectorCalculus):
h := Hessian(p(x, y), [x, y])
Determinant(h)
What I would like to do is to compute the hessian determinant directly with something like
HessDet(p)
where HessDet would be a custom command that performs the operations above. How does one achieve something like this in Maple?
First things first: The value assigned to your p is a procedure which can return a polynomial expression, but not itself a polynomial. It's important not to muddle expressions and procedures. Doing so is a common cause of problems for new users.
Being able to throw around p(x,y) may be visually pleasing to your eye, but it serves little programmatic purpose here. The fact that the formal parameters of procedure p happen to be called x and y, along with the fact that you called procedure p with arguments x and y, is actually just another common source of confusion. Don't create procedures merely to call them in this way.
Also, your call p(x,y) makes it look magic that your code snippet "knows" how many arguments would be required by procedure p. So it's already a muddle to have your candidate HessDet accept p as a procedure.
So instead let's keep it straightforward, by writing HessDet to accept a polynomial rather than a procedure. We can programmatically ascertain the names in which this expression of of type polynom.
restart;
HessDet:=proc(p::algebraic)
local H,vars;
vars:=indets(p,
And(name,Non(constant),
satisfies(u->type(p,polynom(anything,u)))));
H:=VectorCalculus:-Hessian(p,[vars[]]);
LinearAlgebra:-Determinant(H);
end proc:
Now some examples of using it,
P := x^2*y + 3*x^3 + y^3;
HessDet(P);
p := (x, y) -> x^2*y + 3*x^3 + y^3;
HessDet(p(x,y));
HessDet(x^3-x^2+4*x);
HessDet(s^2*t + 3*s^3 + t^3);
HessDet(s[r]^2*t[r] + 3*s[r]^3 + t[r]^3);
You might also wonder how you could re-use this custom procedure across sessions, without having to type it in each time. Two reasonable ways are:
Put the (above) defining plaintext definition of HessDet inside a personal initialization file.
Create a (.mla) Maple Library Archive file, then Save your HessDet to that, and then augment the Library search path in your initialization file.
It might look like 2) is more effort, but only the Save step is needed for repeats, and you can store many custom procedures to the same archive. Your choice...
[edit] The OP has asked for clarification of the first part of the above procedure HessDet, which I suspect means the call to indets.
If P is assigned an expression then then the call indets(P,name) will return a set of all the names present in that expression. Basically, it returns the set of all indeterminate subexpressions of the expression which are of type name in Maple's technical sense.
For example,
P := x*y + sin(a*Pi)*x;
x y + sin(a Pi) x
indets( P,
name );
{Pi, a, x, y}
Perhaps the name of the constant Pi is not wanted here. Ie,
indets( P,
And( name,
Non(constant) ) );
{a, x, y}
Perhaps we want only the non-constant names in which the expression is a polynomial? Ie,
indets( P,
And( name,
Non(constant),
satisfies(u->type(p,polynom(anything,u))) ) );
{x, y}
That last result is an advanced way of using the following tests:
type(P, polynom(anything, x));
true
type(P, polynom(anything, y));
true
type(P, polynom(anything, a));
false
A central issue here is that the OP made no mention of what kind of polynomials are to be handled by the custom procedure. So I guessed with some defensive coding, in hope of less surprises later on. The original Question states that the input could be a "polynomial", but we weren't told what kind of coefficients there might be.
Perhaps the coefficients will always be real and exact or numeric. Perhaps the custon procedure should throw an error when not supplied such. These details weren't mentioned in the Question.

Indefinite integration with Matlab's Symbolic Toolbox - complex solution

I'm using Matlab 2014b. I've tried:
clear all
syms x real
assumeAlso(x>=5)
This returned:
ans =
[ 5 <= x, in(x, 'real')]
Then I tried:
int(sqrt(x^2-25)/x,x)
But this still returned a complex answer:
(x^2 - 25)^(1/2) - log(((x^2 - 25)^(1/2) + 5*i)/x)*5*i
I tried the simplify command, but still a complex answer. Now, this might be fixed in the latest version of Matlab. If so, can people let me know or offer a suggestion for getting the real answer?
The hand-calculated answer is sqrt(x^2-25)-5*asec(x/5)+C.
This behavior is present in R2017b, though when converted to floating point the imaginary components are different.
Why does this occur?
This occurs because Matlab's int function returns the full general solution when you ask for the indefinite integral. This solution is valid over the entire domain of of real values, including your restricted domain of x>=5.
With a bit of math you can show that the solution is always real for x>=5 (see complex logarithm). Or you can use more symbolic math via the isAlways function to show this:
syms x real
assume(x>=5)
y = int(sqrt(x^2-25)/x, x)
isAlways(imag(y)==0)
This returns true (logical 1). Unfortunately, Matlab's simplification routines appear to not be able to reduce this expression when assumptions are included. You might also submit this case to The MathWorks as a service request in case they'd consider improving the simplification for this and similar equations.
How can this be "fixed"?
If you want to get rid of the zero-valued imaginary part of the solution you can use sym/real:
real(y)
which returns 5*atan2(5, (x^2-25)^(1/2)) + (x^2-25)^(1/2).
Also, as #SardarUsama points out, when the full solution is converted to floating point (or variable precision) there will sometimes numeric imprecision when converting from exact symbolic form. Using the symbolic real form above should avoid this.
The answer is not really complex.
Take a look at this:
clear all; %To clear the conditions of x as real and >=5 (simple clear doesn't clear that)
syms x;
y = int(sqrt(x^2-25)/x, x)
which, as we know, gives:
y =
(x^2 - 25)^(1/2) - log(((x^2 - 25)^(1/2) + 5i)/x)*5i
Now put some real values of x≥5 to check what result it gives:
n = 1004; %We'll be putting 1000 values of x in y from 5 to 1004
yk = zeros(1000,1); %Preallocation
for k=5:n
yk(k-4) = subs(y,x,k); %Putting the value of x
end
Now let's check the imaginary part of the result we have:
>> imag(yk)
ans =
1.0e-70 *
0
0
0
0
0.028298997121333
0.028298997121333
0.028298997121333
%and so on...
Notice the multiplier 1e-70.
Let's check the maximum value of imaginary part in yk.
>> max(imag(yk))
ans =
1.131959884853339e-71
This implies that the imaginary part is extremely small and it is not a considerable amount to be worried about. Ideally it may be zero and it's coming due to imprecise calculations. Hence, it is safe to call your result real.

Computing mixed derivatives in MATLAB using syms and diff

I'm using MATLAB 2012b.
I want to get d²/dxdy of a simple function:
f(x,y) = (x-1)² + 2y²
The documentation states that I can use syms and diff as in the following example:
> syms x y
> diff(x*sin(x*y), x, y)
ans =
2*x*cos(x*y) - x^2*y*sin(x*y)
But doing the same I got the wrong answer:
> syms x y
> f = (x-1)^2 + 2*y^2;
> diff(f,x,y)
ans =
4*y
The answer is right if I use diff like this:
diff(diff(f,x),y)
Well, it's not a problem for me to use it this way, but nevertheless why is the first variant not working? Is it a version issue?
The actual documentation from R2010a:
diff(expr) differentiates a symbolic expression expr with respect to its free variable as determined by symvar.
diff(expr, v) and diff(expr, sym('v')) differentiate expr with respect to v.
diff(expr, n) differentiates expr n times. n is a positive integer.
diff(expr, v, n) and diff(expr, n, v) differentiate expr with respect to v n times.
So, the command diff(f,x,y) is the last case. It would be equal to differentiating f w.r.t. x, y times, or w.r.t y, x times.
For some reason I don't quite understand, you don't get a warning or error, but one of the syms variables gets interpreted as n = 1, and then the differentiation is carried out. In this case, what diff seems to do is basically diff(f, y, 1).
In any case, it seems that the behavior changed from version to version, because in the documentation you link to (R2016b), there is an additional case:
diff(F,var1,...,varN) differentiates F with respect to the variables var1,...,varN
So I suspect you're running into a version issue.
If you want to differentiate twice, both w.r.t x and y, your second attempt is indeed the correct and most portable way to do that:
diff( diff(f,x), y )
or equivalently
diff( diff(f,y), x )
NB
I checked the R2010a code for symbolic/symbolic/#sym/diff.m and indeed, n is defaulted to 1 and only changed if one of the input variables is a double, and the variable to differentiate over is set equal to the last syms variable in the argument list. The multiple syms variable call is not supported, nor detected and error-trapped.
Syms is only creating symbolic variables.
The first code you execute is only a single derivative. The second code you provided differentiates two times. So I think you forgot to differentiate a second time in the first piece of code you provided.
I am also wondering what answer you expect? If you want 4*y as answer, than you should use
diff(f,y)
and not
diff(f,x,y)
Performing the second derivative is giving me zero?
diff(diff(f,x),y)
If you want 4 as answer than you have to do following:
diff(diff(f,y),y)

Matlab - Symbolically Solving Inequalities

So, I'm trying to establish some new stability criteria for my simulations, and this involves a lot of convoluted inequalities. I've worked through the math a few times by hand, and it's very laborous; so, I wanted to figure out a way to automate the process (as I'm trying to find the best integration scheme from a stability perspective). Is there anyway to solve inequalities symbolically in Matlab? Here's what I'm trying to solve. In the following expression, x refers to the gradient of a force function with respect to x, and t is the time step. In general, x < 0 and t > 0:
-(t*x + (2*t^3*x + t^2*x^2 - 2*t*x + 4*t + 1)^(1/2) + 1)/(x*t^2 - 2) < 1
Based on what I've looked at online, this seems to be possible in MuPAD, but using the following code does not give me any valid results:
solve(-(t*x + (2*t^3*x + t^2*x^2 - 2*t*x + 4*t + 1)^(1/2) + 1)/(x*t^2 - 2) < 1, t)
Any idea what I can do to make this work and automate the process?
First, since Wolfram Alpha gives you an answer (that I presume you've checked for correctness), I assume you want to use Matlab to solve other similar problems. However, this is a very non-trivial inequality due to the roots of the polynomials. I haven't been able to get Matlab/MuPAD to do anything with it. As I stated, regular Matlab can solve inequalities and systems of equalities in many cases, e.g., in R2013b
syms x real;
solve(x^3-1>1,x)
Even Mathematica 9 has trouble (the Reduce function can be used instead of it's Solve, but the output is not easy to use).
You can, however, solve for the real roots where x < 0 and t > 0 via
syms x t real;
assume(x<0);
assume(t>0);
f = -(t*x+sqrt(2*t^3*x+t^2*x^2-2*t*x+4*t+1)+1)/(x*t^2-2);
s = solve(f==1,t)
which returns:
s =
-(x + (x*(x - 2))^(1/2))/x
This simplifies to sqrt((x-2)/x)-1. Thus t > sqrt((x-2)/x)-1, one of the bounds provided by Wolfram Alpha. (The other more complicated bound is always less than zero and actually is the condition that ensures that t is real.)
But, do you even need to be solving this problem symbolically? Do you need explicit expressions for the various intervals in terms of all x? If not, this type of problem is probably better suited numeric approaches – either via root solving (e.g., fzero) or minimization (e.g., fmincon).

Tidy up expression after differentiating

The equations I am working with in this problem contain exponentials. For this reason, after differentiating they appear again pretty much unchanged apart from additional constants and other factors. I was wondering if it might be possible to collect those exponentials back into the name of the expression they where part of. Here is an example:
Given this function definition
f:= x -> A*exp(B/x)
After differentiating, one would get
A*exp(B/x)*(-B/x^2)
And it would be great if I could somehow convert it to
f(x)*(-B/x^2)
Is this possible with MuPad or any other CAS package?
It's easy in MAPLE, use general substitution command algsubs
> f:= x -> A*exp(B/x)
> g := diff(f(x), x)
> algsubs(A*exp(B/x) = F, g)
For more complex expressions, try collect