In maple how to select one solution manually? - maple

After some complicated integration maple gives a list of solutions defined on different domains of variables. I need to select just one of them. Domains are so complicated that assuming is not helpful: maple runs out of memory trying to figure out how these assumptions correspond to domains he found. However, it is pretty obvious, which solution I need.
Is it possible in maple to extract somehow solution by its number or just drop undefined solutions making maple to forget about domain where it is defined?
P.S. It is hard to copy-paste this solution as it is pretty long.
UPD Minimal working example:
> sln := int(1/x, x=a..b,AllSolutions):
> value(sln) assuming a>0, b>0;
{ -ln(a) + ln(b) a < b
{
{ 0 b = a
{
{ -ln(a) + ln(b) b < a
In this patricular example adding assuming a<b would help, but I would like to get ln(b)-ln(a) directly.

Have a look at convert. It can take your piecewise system and transform it to an array.
> sln := int(1/x, x=a..b,AllSolutions):
> s:=value(sln) assuming a>0, b>0;
{ -ln(a) + ln(b) a < b
{
s := { 0 b = a
{
{ -ln(a) + ln(b) b < a
> conv:=convert(s,list);
conv := [a < b, -ln(a) + ln(b), b = a, 0, b < a, -ln(a) + ln(b)]
> conv[2];
-ln(a) + ln(b)
You can select your favorite part by giving the right (even) index into the array or by matching the odd ones for the part you want (and then select the corresponding even one).

Related

Does IEC-61131 Structured Text allow comparison of boolean operands?

I'm building a parser and type checker for Structured Text. ST is a derivative of Pascal.
It is clear that ST allows equality comparison of two declared real variables X and Y as
X = Y
It is also clear you can write
X <> Y
and
X > Y
If I have two declared boolean variables A and B, is
A = B
legal? Pascal would certainly say so. The reference documents I have for ST (including an Australian version of the 2004 standard, and several vendors implementations) are unclear.
Can I write:
A > B
and what does it mean?
[In the abstract, I'm interested in the same questions for comparing strings. Brownie points for addressing that issue too].
[No, I can't just try it on a real controller; I don't actually have one and the nearest one is effectively two days away from me.]
What's the answer, and what's the reference document you consulted that shows the answer?
The answer to this question really depends on IDE. Although there is a standard for ST, every vendor implement it little bit differently.
In general this is valid statement.
VAR
a, b: BOOL;
END_VAR
IF a = b THEN
// Do something
END_IF
Here is what is in IEC 61131-3 draft. Unfortunately it is not open document and costs money that is why I cannot post it here or provide a link.
https://webstore.iec.ch/publication/4552
GT > Decreasing sequence: OUT := (IN1>IN2) & (IN2>IN3) & ... & (INn-1 > INn)
GE >= Monotonic sequence: OUT := (IN1>=IN2)&(IN2>=IN3)& ... & (INn-1 >= INn)
EQ = Equality: OUT := (IN1=IN2) & (IN2=IN3) & ... & (INn-1 = INn)
LE <= Monotonic sequence: OUT := (IN1<=IN2)&(IN2<=IN3)& ... & (INn-1 <= INn)
LT < Increasing sequence: OUT := (IN1<IN2) & (IN2<IN3) & ... & (INn-1 < INn)
NE <> Inequality (non-extensible) OUT := (IN1 <> IN2)
This also means that in some IDEs you can use
IF EQ(a, b) THEN
// Do something
END_IF
And this should be valid as well.
Can I write:
A > B
and what does it mean?
If A greater than B this expression will return TRUE otherwise FALSE.

Scala: property based testing: how to know all necssary test cases when writing test

I'm reading about Property based testing using Scala language. In this slide, they present this concept: For proving function a+b is true. we just only to prove those statements are true on random data:
a + b = b + a
a + 0 = a
a + 1 + 1 = a + 2
My question is: Which methodologies for checking that our test cases are enough, and can cover all cases on different data. For example on previous example, how can we sure that after our three properties run correctly, we can sure that our implementation is right.
First of all, I assume, you have a typo in #3, it's supposed to be + rather than *.
To answer your question, you most certainly can not be sure that your implementation is right if you prove these three properties. Consider this implementation for instance, that satisfies all three properties, but is definitely wrong:
def wrongPlus(a: Int, b: Int) = if(a < 3 || b <3) b a+b else 0
To definitively prove the (integer) addition, you need to have an independent implementation of next integer. Then, by definition:
1. a + 0 = a
2. a + next(b) = next(a + b)
If these properties hold for any a and b and some operation +, then + is indeed the addition.

Multiple output under minizinc

Try to learn minizinc but after going through examples, may I just confirm that I actually have to write some procedural language if I want to get multiple output or there is a more "natural to minizinc" way to get it.
For example, suppose I want to have all distinct digits add up to 3 the answers should be 0+3 1+2 2+1 3+0 ...
My mininzinc here:
% how to generate more than one result meeting the constraints
int: n=3;
var 0..9: a;
var 0..9: b;
include "alldifferent.mzn";
constraint all_different([a, b]);
constraint a + b = n;
solve satisfy;
output [
"a + b = n \t\n",
show(a), " + ",
show(b), " = ",
show(n)];
produce only 3+0. How to get to the other answers? Thanks for any advice in advance.
I looked at a post for minizinc 1.6 and it seemed to say left out the output statement would produce all the output (Easy way to print full solution (all decision variables) in minizinc). It does not work. Only one is output.
First of all, the default is to print all variables and their values for a solution, not all solutions.
Use the option -a to get all solutions. mzn-gecode --help to see all options. In your case mzn-gecode -a test.mzn which gives:
a + b = n
3 + 0 = 3
----------
a + b = n
0 + 3 = 3
----------
a + b = n
2 + 1 = 3
----------
a + b = n
1 + 2 = 3
----------
==========
Under configuration there is an option to change the default from printing the first solution after satisfaction. Change it to user-defined-behaviour: print all solutions ... You can have output statement, btw, as well.

Immediately invoked named functions

A friend of mine posed an interesting question to me today about how to write immediately invoked named functions in CoffeeScript without hoisting the function variable to the outer scope.
In JavaScript:
(function factorial(n) { return n <= 1 ? 1 : n * factorial(n-1); })(5);
The best I could come up with in CoffeeScript:
do -> do factorial = (n = 5) ->
if n <= 1 then 1 else n * factorial(n-1)
looks a bit awkward. Is there a better way to do this?
You can’t. CoffeeScript doesn’t support this kind of thing at all, except via inline JavaScript:
result = `(function factorial(n) {`
return if n <= 1 then 1 else n * factorial(n-1)
`})(5)`
(No indenting allowed, either.) CoffeeScript will insert some semicolons for you, too, so no using it in expression context.
Then again…
-> if n <= 1 then 1 else n * arguments.callee n-1
(don’t do that)

Maple: simplifying Im(a+I*b) - why it does not work for me?

So I want to simplify z:=a+I*b; Im(z) where a, b are real variables So I try:
s:= 1+2*I
Im(s) // outputs 2
z:=a+I*b
Im(z) // outputs Im(a+I*b)
So I wonder is it any how possible to simplify Im(z) so to get b as output (here we look at general case meaning z could be any complex expression from real values (like a, b, c etc and complex I))?
You didn't tell Maple that a and b were real, so the simplification doesn't work because it doesn't necessarily hold. One way to get what you want is by using the assume command to let it know:
> s:=1+2*I;
s := 1 + 2 I
> Im(s);
2
> z:=a+I*b;
z := a + b I
> Im(z);
Im(a + b I)
> assume(a,real);
> assume(b,real);
> z;
a~ + b~ I
> Im(z);
b~
The evalc command works by considering unknowns as being real.
z:=a+I*b:
Im(z);
Im(a + I b)
evalc( Im(z) );
b
See its help-page, ?evalc.