Kitonum

21835 Reputation

26 Badges

17 years, 221 days

MaplePrimes Activity


These are answers submitted by Kitonum

Custom procedure  P  makes it faster:

restart;

P:=proc(M1::posint,M2::posint)

local A, B, k, r, c;

A:=M1; B:=M2;

k:=0:

while B>0 do

r:=irem(A,B,'q'):  A:=B:  B:=r:

k:=k+1: c[k]:=q:

od:

convert(c, list);

end proc:

 

M1:=146996733613391:

M2:=1348471408813:

teks:= CodeTools:-Usage(P(M1,M2));

    

I did not realize what time you want to reduce. If you need a custom code that solves the same problem, here it is

restart;

M1:=146996733613391:

M2:=1348471408813:

k:=0:

while M2>0 do

r:=irem(M1,M2,'q'):  M1:=M2:  M2:=r:

k:=k+1: c[k]:=q:

od:

convert(c, list);

                                 [109, 101, 115, 115, 97, 103, 101]

t:= `Hello Bob  `;

cat(t$5);

Slightly more efficient will be the enumeration in a nested seq . Also I use LinearAlgebra[Determinant] mod 2 :

restart;

NestedSeq:=proc(Expr::algebraic, L::list)

local S;

eval(subs(S=seq, foldl(S, Expr, op(L))));

end proc:

M:=[i||(1..16)]:

GROUP:=[NestedSeq('`if`(LinearAlgebra[Determinant](Matrix(4, M)) mod 2<>0, cat(op(M)), NULL)',M=~0..1)]:

nops(GROUP);

                                                      20160

 

Perhaps you have confused  YP__1  with  YP[1] . These are different things. The first is a simple symbol, which look like an indexed symbol, the second is a true indexed symbol.  Compare what they look like in output:

restart;

whattype ~ ([YP__1, YP[1]]);

YP__1, YP[1];

                                

 

If they are not confused with one another, then everything is OK:

Eq:= YP[1]=Y[2]:

subs(YP[1]=dGx, Eq);

or

Eq:= YP__1 = Y__2:

subs(YP__1=dGx, Eq);

 

 

 

 

If some elements are repeated in a list,  ListTools:-SearchAll  command finds all the positions:

L := [x,z,y,x,x,z]:

ListTools:-SearchAll(x, L);

                                            1, 4, 5

 

Similar commands there are in  StringTools  package.

This is a less elegant way than the previous 2 methods, but it is conceptually simpler and more general, since it allows you to apply some manipulations to an explicitly specified elements of a list.

A := [[2,3], [4,5], [6,7]]:

[seq(A[i,1]/A[i,2], i=1..nops(A))];

                                                [2/3, 4/5, 6/7]

 

The same can be done not only with the lists, but with the other objects (sequences, sets, vectors, matrices, etc.)

For the column names you can use uppercase letters, for example,  U0, U1, U2  and so on. Maple distinguishes between uppercase and lowercase letters.

LengthOfIntersection procedure finds in the form of an explicit piecewise function the length intersection of 2 segments (real closed intervals) (see the original question). Now we can not only find this length for a given x, but also differentiate, integrate, etc.

LengthOfIntersection := proc (x::name, A::list(realcons), B::list(realcons))

local h, B1, d;

h := min(A[2]-A[1], B[2]-B[1]);

d := abs(B[2]-B[1]-A[2]+A[1]);

B1 := B+~x;

piecewise(convert(solve(B1[2] <= A[1], {x}), `and`), 0,

convert(solve({B1[2] < A[1]+h, A[1] < B1[2]}, {x}), `and`), B1[2]-A[1],

convert(solve({B1[2] <= A[1]+h+d, A[1]+h <= B1[2]}, {x}), `and`), h,

convert(solve({B1[1] < A[2], A[1]+h+d < B1[2]}, {x}), `and`), A[2]-B1[1],

convert(solve(A[2] <= B1[1], {x}), `and`), 0);

end proc:

 

Examples of use:

f := unapply(LengthOfIntersection(x, [1, 3], [0, 5]), x):

f(x);

f(2);

diff(f(x), x);

int(f(x), x = -4 .. 3);

plot(f, -5..5, color=red, thickness=3, scaling=constrained);

                                                     

 

                                              

 

You can do 

for i to 4 do i^2 end do;

i;

i := 'i'; 

i;

 

Now  i  is symbol

whattype(i);

                                  symbol

Alternative option:

V:=[a5, a6, a7]:

map(t->[t,0], V):

seq(seq(seq([i,j,k], k=%[3]), j=%[2]), i=%[1]);

    

 

 

m:= [01100101, 01101100, 01100111, 01100001];

map(t->[seq(parse(convert(t,string)[i]), i=1..length(convert(t,string)))], m);

         

 or

map(t->parse~([StringTools[LengthSplit](convert(t, string),1)]), m);

 

Addition: If you want to preserve leading zeros, then the original numbers should be directly defined as  strings or symbols, for example:

m:= convert~([`01100101`, `01101100`, `01100111`, `01100001`], string);

map(t->[seq(parse(t[i]), i=1..length(t))], m);

      

 

Otherwise Maple automatically discards these zeros.

 

 

Example:

ListTools[Reverse]([1, 1, 0, 0, 1, 1, 1, 0]);

                                                 [0, 1, 1, 1, 0, 0, 1, 1]

Simple procedure  f  solves your problem:

f:=proc(x, A, B)

local B1;

B1:=[B[1]+x, B[2]+x];

`if`(A[2]<=B1[1] or B1[2]<=A[1], 0, min(A[2],B1[2]) - max(A[1],B1[1]));

end proc:

 

Examples of use:

f(0,[2,4],[0,1]);

f(3/2,[2,4],[0,1]);

f(3,[2,4],[0,1]);

f(4,[2,4],[0,1]);

f(0,[1,3],[0,5]);

plot('f'(x,[1,3],[0,5]), x=-5..5, color=red, thickness=3, scaling=constrained);

                  

 

 

I don't know any Maple procedures which solve the similar problems.

1) LinearAlgebra[LinearSolve]  command finds the infinite number of solutions that depend on the three parameters:

restart;

with(LinearAlgebra):

 l1 := [1, 1, 1, 0, 0, 0, 0, 0, 0]:

l2 := [0, 0, 0, 1, 1, 1, 0, 0, 0]:

l3 := [0, 0, 0, 0, 0, 0, 1, 1, 1]:

l4 := [1, 0, 0, 1, 0, 0, 1, 0, 0]:

l5 := [0, 1, 0, 0, 1, 0, 0, 1, 0]:

l6 := [0, 0, 1, 0, 0, 1, 0, 0, 1]:

l7 := [0, 0, 1, 0, 1, 0, 1, 0, 0]:

A := Matrix([l1, l2, l3, l4, l5, l6, l7]):                                   

b := <15, 15, 15, 15, 15, 15, 15>:  # Vector specified by right hand side of the matrix equation  A.m=b

m:=LinearSolve(A, b);

                                             

 

2) The second problem has 24 solutions:

S:={$ 2..8}:  n:=0:  T:=table():

for i in S do

for j in S do

for k in S do

m0:=eval(m,{_t[6]=i, _t[8]=j , _t[9]=k});

if convert(m0, set)=S then n:=n+1; T[n]:=m0  fi;

od: od: od:

T:=convert(T, set);

nops(T);

 

 

 3) The third problem has no solutions:

S1:={$ 0..8}:

P:=combinat[permute](S1, 3): n:=0: T:=table():

for p in P do

m0:=eval(m,{_t[6]=p[1],_t[8]=p[2],_t[9]=p[3]});

if convert(m0,set)=S1 then n:=n+1; T[n]:=m0 fi;

od:

T:=convert(T,set);

                                                       T := {}

 

But if we replace  {$ 0..8}  by  {$ 1..9)  then again get  24  solutions:

S2:={$ 1..9}:

P:=combinat[permute](S2, 3):  n:=0:  T:=table():

for p in P do

m0:=eval(m, {_t[6]=p[1],_t[8]=p[2],_t[9]=p[3]});

if convert(m0, set)=S2  then n:=n+1; T[n]:=m0  fi;

od:

T:=convert(T, set):

nops(T);

                                                         24

 

Matrix_Equation.mws

First 190 191 192 193 194 195 196 Last Page 192 of 292