Finding the error in this code - maple

I keep receiving error,';' unexpected in this piece of maple code. I have looked and looked and just can't seem to find where I'm going wrong. Can anyone spot it?
QSFactorization := proc (n::(And(posint, odd)), mult::nonnegint := 0, { mindeps::posint := 5, c := 1.5 })
local mfb, m, x, fb, nfb, r, M, d;
if isprime(n) then
return "(n)"
elif issqr(n) then
return "(isqrt(n))"*"(isqrt(n))"
elif n < 1000000 then
return ifactor(n)
end if;
if mult = 0 then
mfb := MultSelect(n, ':-c' = c)
else mfb := [mult, FactorBase(mult*n, c)]
end if;
m := mfb[1];
if 1 < m then
print('Using*multiplier; -1');
print(m)
end if;
x := m*n*print('Using*smoothness*bound; -1');
print(ceil(evalf(c*sqrt(exp(sqrt(ln(n)*ln(ln(n))))))));
fb := Array(mfb[2], datatype = integer[4]);
nfb := ArrayNumElems(fb);
print('Size*of*factor*base; -1');
print(nfb);
r := Relations(x, fb, ':-mindeps' = mindeps);
M := r[3]; print('Smooth*values*found; -1');
print(nfb+mindeps);
print('Solving*a*matrix*of*size; -1');
print(LinearAlgebra:-Dimension(M));
d := Dependencies(M);
print('Number*of*linear*dependencies*found; -1');
print(nops(d));
print('Factors; -1');
FindFactors(n, r, d)
end proc
I'd really appreciate any insight.

You basic problem is that you are using the wrong quotes inside your print statements. This is invalid,
print('Using*multiplier; -1');
You are using single right-quotes (tick), which in Maple is used for unevaluation. In this case the semicolons inside your print statements are syntax errors.
Use either double-quotes or single left-quotes instead. The former delimits a string, and the latter delimits a name. Eg,
print("Using*multiplier; -1");
print(`Using*multiplier; -1`);
If you choose to go with name-quotes then the print command will prettyprint the output in the Maple GUI with the name in an italic font by default, but you won't see the quotes in the output.
If you choose to go with string-quotes then the print command will show the quotes in the output, but will use an upright roman font by default.
Some other comments/answers (since deleted) on your post suggest that you are missing statement terminators (colon or semicolon) for these two statements,
print(m)
FindFactors(n, r, d)
That is not true. Those statements appear right before end if and end proc respectively, and as such statement terminators are optional for them. Personally I dislike coding Maple with such optional terminator instances left out, as it can lead to confusion when you add intermediate lines or pass the code to someone else, etc.

Related

Programming with Maple for Numerical Analysis : Relative error

I am working on some Numerical Analysis with Maple as a part of my course and I am not sure where my error is with the code I am using.. If anyone can point out my flaw It would be very much appreciated as I seem to be getting the answer wrong.
f(x)=sqrt((cosh(x))^2 + 1) - sinh(x).
Find a good approximation to f(4.86) using a 6-digit arithmetic.
Then, Use 20 digit arithmetic to calculate the relative error.
Finally, round it to 6 (significant) digits
f := sqrt(cosh(x)^2+1)-sinh(x);
f1 := evalf[6](f(4.86));
f1 := 0.0155
f2 := evalf(f(4.86));
f2 := 0.01550004
Digits := 20;
Digits := 20
Q4 := abs((f2-f1)/f2);
Q4 := 0.0000025806385015780604437
Digits := 6;
Digits := 6
evalf[6](Q4);
0.00000258064
Thanks everyone
You've made one syntax transcription mistake, and one Maple programming mistake.
Your first line is
f := sqrt(cosh(x)^2+1)-sinh(x);
but you subsequently call it like an operator (procedure), eg. f(4.86) and obtain a numeric value. Therefore you must have originally used something like this procedure, instead.
f := x -> sqrt(cosh(x)^2+1)-sinh(x);
So that was likely just a transcription error when posting here.
You have made a programming mistake, in computing
f2 := evalf(f(4.86));
before setting the working-precision environment variable Digits to 20. Thus you have computed f2 at only the default Digits=10 working precision. But from the wording of the question it seems that you are being asked to compute f2 also at 20 digits of working precision.
Your code might be revised as follows:
restart;
f := x -> sqrt(cosh(x)^2+1)-sinh(x):
f1 := evalf[6](f(4.86));
f1 := 0.0155
Digits := 20:
f2 := evalf(f(4.86));
f2 := 0.015500036806894590
Q4 := abs((f2-f1)/f2);
Q4 := 0.0000023746327217512077767
evalf[6](Q4);
0.00000237463
You have used two different mechanisms for specifying the working precision. You could have also done it (slightly more consistently in methodology) as follows:
restart;
f := x -> sqrt(cosh(x)^2+1)-sinh(x):
f1 := evalf[6]( f(4.86) );
f1 := 0.0155
f2 := evalf[20](f(4.86));
f2 := 0.015500036806894590
rel := evalf[20]( abs((f2-f1)/f2) );
rel := 0.0000023746327217512077767
evalf[6]( rel );
0.00000237463
There's always the possibility that I have misunderstood the question. What is the desired answer?

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.

With Maple how can i prompt the user to answer a boolean expression?

The exact question would be "is the equation you want to use x=f(Xo)". This is in an if statement already so if true then continue if not then prompt user to enter a different function.
Your bit about its already being inside an if statement isn't very workable because that doesn't allow for an alternative value to be assigned to something in the case that the initial response is negative.
You should be able to work with something like this. Call p(), and assign its result to a ans, say, and then work with that value (and/or test it for some properties).
restart:
p := proc()
local answer, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf("Is the equation you want to use x=f(Xo)? (y/n)\n");
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
answer := x=f(Xo);
elif member(res1,{"n;","n","no;","no"}) then
printf("Enter your equation.\n");
res2 := readline(-1);
answer := parse(res2);
else
printf("Response not recognized\n");
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
if answer='answer' then NULL else answer end if;
end proc:
ans := p();
[edited below]
It is possible to get it a little closer to your original. With procedure p as below the returned result will be one of true/false/FAIL and could be used in a conditional. In the case that the return values if false (because of the response to the initial query) then a second query is made about the choice of another expression.
This version of p takes two arguments, the first is the suggested initial equation. The second is a name which can be assigned any alternative.
restart:
p := proc(candidate, resultvar)
local result, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf(sprintf("Is the equation you want to use %a? (y/n)\n",
candidate));
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
result := true;
assign(resultvar,candidate);
elif member(res1,{"n;","n","no;","no"}) then
result := false;
printf("Enter your equation.\n");
res2 := readline(-1);
assign(resultvar,parse(res2));
else
printf("Response not recognized\n");
result := FAIL;
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
return result;
end proc:
Now we can test it out.
p(x=f(X0), 'ans');
ans;
We could also use the call to p inside an if statement. Eg,
if p(x=f(X0), 'ans') then
"accepted";
else
"new choice made";
end if;
ans;
Here, answering "n" to the first query will make the conditional test see a false value, but the named argument ans will get assigned to as a side-effect.

How to concatenate a number and a string in auto hotkey

I have the following auto hotkey script:
A:= 5
B := "7"
C := A.B
MsgBox %C%
The third line does not work.
I'm expecting output of "57"
I have tried the following:
C := %A%.%B%
C := (A).(B)
C := (A.B)
C := (%A%.%B%)
C := (%A%).(%B%)
None of which work
Can anyone tell me how to do it?
I'm using version 1.1.09.04
Just updated to latest version 1.1.14.01 and its still the same
You have distinguish between expressions (:=) and "normal" value assigments (=). Your goal can be met with several approaches, as shown in the following examples:
a := 5
b := 7
x := 6789
; String concatenation
str1 = %a%%b%
; or as an expression
str2 := a b
; or with explicit concatenation operators
str3 := a . b
; Mathematical "concatenation"
; if b has exactly one digit
val1 := a*10 + b
; for any integer
val2 := a * (10**StrLen(x)) + x ; ** is the "power" operator
msgbox, str1 = %str1%`nstr2 = %str2%`nstr3 = %str3%`nval1 = %val1%`nval2 = %val2%
This code will print:
str1 = 57
str2 = 57
str3 = 57
val1 = 57
val2 = 56789
In AHK, all of these methods should be quasi-equivalent: They produce the same kind of output. The mathematical approach marks the variables as numbers, leading to possible trailing zeros, which you may want to Round() before displaying. The output of our string concatenation can be used as a number as well, since AHK auto-boxes them if neccessary. For example, you could calculate
z := str1 - 1
and it would evaluate to 56.
I personally prefer the mathematical approach, since it will result result in an actual number and not a string, which seems only logical.

Maple - Sequence element affectation

I am encountering a problem when manipulating sequence element in Maple. First of all, here is the code.
b[0] := t -> (1-t)^3;
b[1] := t -> 3*t*(1-t)^2;
b[2] := t -> 3*t^2*(1-t);
b[3] := t -> t^3;
P := seq([seq([j*(i+1), j*(i-1)], i = 1 .. 4)], j = 1 .. 3);
EvalGamma := proc (b, P, i, t)
local CP, res;
option trace;
CP := P[i];
res := CP[1]*b[0](t)+CP[2]*b[1](t)+CP[3]*b[2](t)+CP[4]*b[3](t);
RETURN res;
end proc;
The variable P is a sequence of sequence : P[i] is a sequence of four 2D points.
But the affectation CP := P[i]; doesn't do what I want : I don't know why but the result is not P[i] in the procedure.
And the weird thing is that, outside the procedure, the following lines work :
CP := P[1];
CP[1];
I would appreciate any suggestions. Thanks.
I'm assuming you call the procedure as
EvalGamma(b,P,i,t)
The problem you are having is that when P is inserted into the sequence of arguments, the nested sequence of arguments is "flattened" to produce the final argument list. An easy way to fix this is to place the sequence for P inside a list structure. So use
P := [seq([seq([j*(i+1), j*(i-1)], i = 1 .. 4)], j = 1 .. 3)];
Once you do that, I think everything will work as expected.
When you call EvalGamma you cannot pass that global P which is an expression sequence of (three) lists (or lists). If you try and do so then EvalGamma will receive 6 arguments instead of 4 as you intend, because each of the three lists (of lists) in expression sequence P gets interpreted as a separate argument of the call.
Instead, you could create P as a list, ie,
P := [seq([seq([j*(i+1), j*(i-1)], i = 1 .. 4)], j = 1 .. 3)];
or you could pass it like EavlGamma(b, [P], some_i, some_name). But you should only do one of those two choices.
Note that the return syntax should be either return res; or (deprecated) RETURN(res);.