Items tagged with programming

My code

checkInSet := proc(L, x)
    local i;
    if nops(L) = 0 then
        return 0;
    else    
        for i from 1 to nops(L) do
            if x = L[i] then
                return i;
            fi;
        end;
    end;
end proc:

 

Let

M := {(a-2*b-2*c)^2*(2*a-b+2*c)^2, (a-2*b-c)^2*(a-b+2*c)^2, (a-2*b+c)^2*(a+b-2*c)^2, (a-2*b+2*c)^2*(2*a+b-2*c)^2, (a-b-2*c)^2*(2*a-b+c)^2, (a-b-c)^2*(a-b+c)^2, (a-b+c)^2*(a+b-c)^2, (a-b+2*c)^2*(2*a+b-c)^2, (a+b-2*c)^2*(2*a-b-c)^2};

 

But checkInSet(M, (a-2*b-2*c)^2*(2*a-b+2*c)^2) not work? Someone please help me.

Thanks you very much.

Hello!

I was creating this code for the "Müller Method" for Numerical Analysis. Everything works fine till the if (for getting the x3) goes on. The problem is, the Maple doesn't detects the if.
I tried checking every variable and every variable is calculated except "x3"; even "disc".

I would like to know what happened. Anyways, here's the code:
http://imgur.com/a/DGlgl
 

                     
x0 := 0; x1 := 8.4; x2 := 10; iter := 1000; tol := 10^(-8); f := proc (x) options operator, arrow; 3*x^3+7*x^2+x+2 end proc; f(x); plot(f(x), color = green); printf(" n          x0               x1               x2              x3                Error \n"); for i to iter do d0 := evalf((f(x1)-f(x0))/(x1-x0)); d1 := evalf((f(x2)-f(x1))/(x2-x1)); h1 := evalf(x2-x1); h0 := evalf(x1-x0); a := evalf((d1-d0)/(h1-h0)); b := evalf(a*h1+d1); c := evalf(f(x2)); disc := sqrt(-4*a*c+b^2); if abs(b+disc) > abs(b-disc) then x3 := x2+(-2*c)*(1/(b+sqrt(-4*a*c+b^2))); erry := abs((x3-x2)/x3) else x3 := x2+(-2*c)*(1/(b-sqrt(-4*a*c+b^2))); erry := abs((x3-x2)/x3) end if; if erry > tol then x0 := x1; x1 := x2; x2 := x3; printf("%2d     %2.8f      %2.8f       %2.8f         %2.5 f     %2.8 f \n", i, a, b, c, x3, erry) else printf("una raiz es: %2.8f ", x3); break end if end do;

I put the image (as it looks on my maple) and the "code" so you can copy-paste it in Maple.

Let us consider the linear integer programming problem:

A := Matrix([[1, 7, 1, 3], [1, 6, 4, 6], [17, 1, 5, 1], [1, 6, 10, 4]]):
 n := 4; z := add(add(A[i, j]*x[i, j], j = 1 .. n), i = 1 .. n):
restr := {seq(add(x[i, j], i = 1 .. n) = 1, j = 1 .. n), seq(add(x[i, j], j = 1 .. n) = 1, i = 1 .. n)}:
 sol := Optimization[LPSolve](z, restr, assume = binary);

Error, (in Optimization:-LPSolve) no feasible integer point found; 
use feasibilitytolerance option to adjust tolerance

sol1 := Optimization[LPSolve](z, restr, assume = binary, feasibilitytolerance = 100, integertolerance = 1);

Error, (in Optimization:-LPSolve) no feasible integer point found;
 use feasibilitytolerance option to adjust tolerance

That was OK in Maple 16, outputting

.

The bug in one of the principal Maple commands lasts since Maple 2015, where the above code causes "Kernel connection has been lost". The SCRs about it were submitted three times (see http://www.mapleprimes.com/questions/204750-Bug-In-LPSolve-In-Maple-20151).

I found 4 ways so far to call a Maple sin function (to return numerical value).

Can you find more ways?

evalf(sin(Pi/2));
evalf(`sin`(Pi/2));
evalf('sin'(Pi/2));
`evalf/sin`(Pi/2);

 

 

 

 

 

I am maple newbie. Sometimes when I look at Maple code to try to understand the algorithm (which is hard, since I do not know Maple well), I see the code puts ` ` around some keywords. And sometimes it does not. For example, sometimes I see something like (these are random samples) from Maple code shown using showstat()

return `if`(assigned(r),r,{})
r := `union`(r,{solve(op(1,expr) = 1,vs)})
v2 := `intersect`(vs,indets(op(1,expr),'name'));

But sometimes, they do not put ` ` around functions name or keywords, like this:

if nops(v2) = 1 then
t1 := remove(a -> has(a,RootOf),t1);
for t in expr do

and so on.

Can some expert please give what is the rule thumb to use? Should user adopt this method also? When to put ` ` and when not to put ` `?   I understand that ` ` prevents one-time evaluation (or rather, holds off immediate evaluation) and ``  `` prevents two times evaluation and so on. But when to use ` ` is what is confusing me.

 

how to convert a nested for loop to iterative version with stack

I was trying to write a procedure that would compute a simple linear equation using the Extended Euclidean Algorithm. I was thinking of a procedure like the following:

solveEeaMatrix := proc (a::list, b::list) 
 local c::list;  
 c := a -iquo(a[1],b[1])*b;  
 print(c);  
 while (c[1] <> gcd(a[1],b[1]) do 
 ...

I am basically stuck at this part as

1) I don't know how to setup a multi-dimensional array that could dynamically grow(as a possible solution).

2) I can't come up with a recursive function that could possibly take care of this.

In short, if I am given for example an equation like: 84*x+203*y = 14

I will transform it into 2 linear equation as follow:
row0 := [203, 0, 1] row1 := [84, 1, 0] Subsequently, I will perform the following:

c := a -iquo(a[1],b[1])*b;  

Where aand b are both lists and arguments of the procedure and cbeing another list and a local variable.

But I don't know how to do the following programmatically:

row3 := row1-iquo(row1[1], row2[1])*row2;
row4 := row2-iquo(row2[1], row3[1])*row3;
row5 := row3-iquo(row3[1], row4[1])*row4;
and so on ...                  

Any hint would be appreciated.

Please Sir/Ma, I'm trying to generate a recurrent relations of this series and I try to use "if" "else" condition but I didn't get it right. Any one with useful suggestions. Appreciate 

 

restart;
Y[0] := A; Y[1] := B;
if k = a then delta(k-a) := 1 else 0 end if;
                               0
for k from 0 to 10 do Y[k+2] := solve(add(delta(i-1)*(k-i+1)*(k-i+2)*Y[k-i+2], i = 0 .. k)+add((delta(i)-delta(i-1))*(k-i+1)*Y[k-i+1], i = 0 .. k)+lambda*Y[k] = 0, Y[k+2]) end do;
y := sum(Y[j]*x^j, j = 0 .. 10);
 

Hello,

So as to build a function which gives several outputs, i have made a code with this manner :

Input:=[i1, ..., in]

Output:=proc(Input);

      export o1, o2, o3
      o1=f1(Input);
      o2=f2(Input);
      o3=f3(Input);
      end module

End proc;

By doing, output1:=Output(Input):-o1; , i obtained my result. However, as my structure begins with the procedure (proc End proc), I can't export my result in package.

For this reason, I'm thinking about changing the structure of my code by starting with the creation of the module and put all the functions inside.

Input:=[i1, ..., in]

Output:=module():

export o1,o2,o3;
option package;
o1:=proc(Input)
f1(Input);
end proc;

o2:=proc(Input)
f2(Input);
end proc;

o3:=proc(Input)
f3(Input);
end proc;
   
End module;

Can you give me your feedback on the two structures ? Do you think that the second choice Module->Functions is more appropriated ?

Thanks a lot of your help and feedback.

So below is a calculation im trying to create, but it just tells me there is an unexpected IF error, and returns me to the third For line. I have tried it straight and as a procedure but i just cant seem to get it to work. Any ideas or tweaks to make this work would be much appreciated. All of the other variables I've named work in their respective lines of code, which I have left out as it is long as it is!


ComlexAlgorithm:= proc(L)
local N, j, kf, o, DeltaMag, DeltaBond;
global NConfig, Configz1, Col, Row, OldConfig, NewConfig, NewTable;

# Set Delta values to zero.
DeltaMag:=0:
DeltaBond:=0:

# Creates loops to loop through the correct changes in values i.e. Add -1, then 1 to first point, move across each point one by one, repeat from the beginning for each configuration then repeat whole process for the amount of rows.
printlevel:=4:
for N from -1 to 1 by 2 do
   for j from 1 to Col do
      for w from 0 to NConfig-1 do   
         for o from 1 to Row do

# Calculate DeltaMag.
if N=-1 then DeltaMag:=DeltaMag+1 else DeltaMag:=DeltaMag-1
end if:

# Calculate all relevant bonds i.e. 3 values for DeltaMag, except at "corner" lattice points where there is only 2 bonds.
if N= Configz1[w](o, j) then DeltaBond:= DeltaBond-1 else DeltaBond:= DeltaBond+1
end if:

if j-1>1 then
   if N <> Configz1[w](o, j+1) then DeltaBond:= DeltaBond+1
      if N = Configz1[w](o, j+1) then DeltaBond:= DeltaBond-1
else DeltaBond:= DeltaBond
end if:
end if:
end if:

if j+1<Col then
   if N <> Configz1[w](o, j+1) then DeltaBond:= DeltaBond+1
      if N = Configz1[w](o, j+1) then DeltaBond:= DeltaBond-1
else DeltaBond:= DeltaBond
end if:
end if:
end if:

# Calculate if, and by what degree, conifguration number changes using 2^(j-1) which is the arithmetric series for binary. Changes only occur when "incoming" spin is different.
if N= -1 then
   if Configz1[w](o, j) <> N then NewConfig[w]:= OldConfig[w-(2^(j-1))] 
else NewConfig[w]:= OldConfig[w]
end if:
end if:

if N= 1 then
   if Configz1[kf](o, j) <> N then NewConfig[w]:= OldConfig[w+(2^(j-1))] 
else NewConfig[kf]:= OldConfig[w]
end if:
end if:

NewTable[kf]:= [NewConfig[w], BondEnergy(w)+DeltaBond, MagEnergy(w)+DeltaMag];

# Set Delta values to zero to finsh.
DeltaMag:=0:
DeltaBond:=0:

end do:
end do:
end do:
end do:
end proc:

On some platforms, my editor of choice has become the aptly named Sublime Text. Unfortunately, it does not seem to have built in syntax highlighting for the Maple programming language and so I set out to write some.  In the end, I wrote enough highlighting to keep me sane when looking at Maple source, but it could use a lot more work.  So in case anyone is interested I've put what I have in a Github repository: SublimeTextMaple

If you use Sublime Text, please download it and add your own enhacements and share in turn.

I Could Not Write An If Then Or Ifelse Statement. Please Help Me.

f := unapply(x^2-2, x); a := 1; b := 2; n := 10; Digits := 10;
      2    
x -> x  - 2
                               1
                               2
                               10
                               10
c := evalf(eval((a*f(b)-b*f(a))/(f(b)-f(a))));
                          1.333333333
if  f(c)*f(a)<0 then ;
          "      k:=evalf(eval(|(f(c))/(b-c)|)) and "

                          /(1 + k) a f(b) - b f(a)\
             x[i] := evalf|-----------------------|
                          \  (1 + k) f(b) - f(a)  /
            "     elif f(x[i])*f(a)<0 then b:=x[i]"
                 "     else b:=c and a:=x[i] "
                  "     if f(c)*f(a)>0 then "
                 "      k:=|(f(c))/(b-c)|and "

                          /a f(b) - b f(a) (1 + k)\
             x[i] := evalf|-----------------------|
                          \  f(b) - f(a) (1 + k)  /
            "     elif f(x[i])*f(a)>0 then a:=x[i]"
              "     else a:=c and b:=x[i] end if"

Error, unterminated 'if' statement
     Typesetting:-mambiguous(Typesetting:-mambiguous(

       if fApplyFunction(c)sdotfApplyFunction(a)lt0 then , 

       Typesetting:-merror("unterminated 'if' statement")))

I am trying to implement Subresultant p.r.s. algorithm for calculating greatest common divisor. The algorithm decribed in the book:

My code return the correct GCD, however the sub-resultant terms are different from the result of the built-in function. The last term a[i-1] is huge and involves fractions. I think my implementation is same as the algorithm described in the textbook.

I have attached the file. Could anybody spot anything wrong in the code? Why do fractions still appear? In my code, "lsr" is last subresultant term returned from the built-in function, the second one is my result.

with(RegularChains);

[Chain, ChangeOfOrder, Construct, Cut, DahanSchostTransform, Dimension, Empty, EqualSaturatedIdeals, EquiprojectableDecomposition, Extend, ExtendedNormalizedGcd, IsAlgebraic, IsEmptyChain, IsInRadical, IsInSaturate, IsIncluded, IsPrimitive, IsStronglyNormalized, IsZeroDimensional, IteratedResultant, LastSubresultant, Lift, ListConstruct, NormalizeRegularChain, NumberOfSolutions, Polynomial, Regularize, RemoveRedundantComponents, SeparateSolutions, Squarefree, SquarefreeFactorization, SubresultantChain, SubresultantOfIndex, Under, Upper]

(1)

A42vlastsub := proc (f, g) local i, a, dt, bt, om; i := 1; if degree(f) < degree(g) then a[0] := primpart(g, x); a[1] := primpart(f, x) else a[0] := primpart(f, x); a[1] := primpart(g, x) end if; dt[0] := degree(a[0])-degree(a[1]); bt[2] := (-1)^(dt[0]+1); om[2] := -1; while a[i] <> 0 do a[i+1] := normal(prem(a[i-1], a[i], x)/bt[i+1]); dt[i] := degree(a[i])-degree(a[i+1]); i := i+1; om[i+1] := (-lcoeff(a[i-1]))^dt[i-2]*om[i]^(1-dt[i-2]); bt[i+1] := -lcoeff(a[i-1])*om[i+1]^dt[i-1] end do; return a[i-1] end proc;

 

(2)

f := (y^2-1)*((y+1)*x^4+(y^2-1)*x^3+(y^3-1)*x^2+(y^4-1)*x+y^5-1);

(y-1)*x^5+(y^2-1)*x^4+(y^3-1)*x^3+(y^4-1)*x^2+(y^5-1)*x+y^6-1

(3)

R := RegularChains:-PolynomialRing([y, x]);

subresultant_chain

(4)

lsr := LastSubresultant(src, R);

y^25+y^24+2*y^23+4*y^22+8*y^21+16*y^20+46*y^19+160*y^18+402*y^17+808*y^16+1384*y^15+2080*y^14+2932*y^13+3762*y^12+4406*y^11+4740*y^10+4720*y^9+4400*y^8+3810*y^7+2968*y^6+2102*y^5+1360*y^4+800*y^3+400*y^2+139*y+21

(5)

``

mylastsr := A42vlastsub(primpart(f, x), primpart(g, x));

-(35867/3794275180128377091639574036764685364535950857523710002444946112771297432041422848)*y^9-(10309/7588550360256754183279148073529370729071901715047420004889892225542594864082845696)*y^8-(2889061/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^10-(94304133/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^13-(35600337/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^12-(11265153/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^11-(4325932673/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^21-(3534515779/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^20-(2703789263/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^19-(1929251163/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^18-(1277273509/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^17-(778538921/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^16-(432069123/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^15-(215109057/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^14-(5255652033/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^25-(5374732281/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^23-(5474736805/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^24-(4971065401/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^22-(475/3794275180128377091639574036764685364535950857523710002444946112771297432041422848)*y^7-(21/3794275180128377091639574036764685364535950857523710002444946112771297432041422848)*y^6-(332387607/30354201441027016733116592294117482916287606860189680019559568902170379456331382784)*y^32-(1/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^49-(23/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^48-(251/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^47-(1735/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^46-(8571/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^45-(32463/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^44-(99205/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^43-(255999/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^42-(586005/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^41-(1263605/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^40-(2747253/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^39-(6322305/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^38-(15325169/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^37-(37286331/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^36-(86630947/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^35-(186556683/60708402882054033466233184588234965832575213720379360039119137804340758912662765568)*y^34-(92016457/15177100720513508366558296147058741458143803430094840009779784451085189728165691392)*y^33-(275974877/15177100720513508366558296147058741458143803430094840009779784451085189728165691392)*y^31-(847698927/30354201441027016733116592294117482916287606860189680019559568902170379456331382784)*y^30-(1210953247/30354201441027016733116592294117482916287606860189680019559568902170379456331382784)*y^29-(1616246617/30354201441027016733116592294117482916287606860189680019559568902170379456331382784)*y^28-(505494959/7588550360256754183279148073529370729071901715047420004889892225542594864082845696)*y^27-(2376326883/30354201441027016733116592294117482916287606860189680019559568902170379456331382784)*y^26

(6)

``


 

Download subresultant.mw

How can I ask Maple to wake me up after running the code?

It takes a long time to finish the job. So it would be helpful to play a beep or something to inform me the answer is ready.

Thank you in advance for your help.

hi i have this code that doesnt work and I cant find the problem. I cant make the matrix include more than 2 rows. i want this to give me the whole nxn matrix, and then the solution to the wronskian. 

can you pls help?:) thanks! 

with*linalg;
funcs := [x, 3*x, x^2, 5*x];
n := nops(funcs);
print*n;
printlevel := n;
count := 1;
listM[count] := funcs;
for i from 2 to n do listN := diff(funcs, `$`(x, i-1));
count := count+1;
listM[count] := listN end do;
M := convert(listM, matrix);
det(M)

1 2 3 4 5 6 7 Last Page 1 of 11