alec's blog

alec's picture

Lexical Table

In the recent discussion about patching, a question about patching a function f including local variables of a module or another function was discussed. For example, let it be defined as

A:=proc() global f,t; local x,y,z;
f:=()->x+y();
y:=()->z;
t:=()->x+z;
x,z:=0,1;
NULL end:
A();

Now,

op(f);
                            () -> x + y()
f();
                                  1

How to change it so that it would return 2 instead of 1, without reassigning it?

alec's picture

Maple Wiki

Everybody is invited to Maple Wiki .

It is hosted on Maple Advisor, a Maple community site independent of Maplesoft and/or Mapleprimes.

The site has started just a couple of days ago and doesn't have much of a content yet.

alec's picture

Bernoulli Distribution with Serial Correlation

A random sample of length n drawn from Bernoulli distribution with probability of success prob, that has a correlation c with itself shifted back lag steps, can be generated using following procedure,

SampleWithCorr:=proc(prob::And(numeric,satisfies(c->c>=0 and c<=1)),
lag::posint,c::And(numeric,satisfies(c->c<=1 and c>=-1)),n::posint)
local X,B,S,C,s,i;
uses Statistics;
X:=RandomVariable(Bernoulli(prob));
S:=Sample(X,n);
if n<=lag or s=0 then S else
s:=signum(c);
B:=RandomVariable(Bernoulli(abs(c)));
C:=Sample(B,n-lag);
if s=1 then 
for i from lag+1 to n do
if C[i-lag]=1 then S[i]:=S[i-lag] fi od;
else for i from lag+1 to n do
if C[i-lag]=1 then S[i]:=1-S[i-lag] fi od
fi fi; S end:
alec's picture

Discontinuous antiderivatives

Maple, Mathematica, and many other CAS define indefinite integral up to piecewise constants. That means, in particular, that the integral of a continuous function can be discontinuous.

In many cases that can be easily fixed, such as for integrals involving floor(x) and frac(x). However, it has not been done neither in Maple nor in Mathematica. Here is another example, suggested by David W. Cantrell

int(1/(2+cos(x)),x);

               2/3*3^(1/2)*arctan(1/3*tan(1/2*x)*3^(1/2))
alec's picture

A125033

Sequence A125033 in the OEIS submitted by Paul D. Hanna and Richard J. Mathar, is defined as

Number of labeled nodes in generation n of a rooted tree where a node with label k has k child nodes with distinct labels, such that each child node is assigned the least unused label in the path that leads back to the root node with label '1'.

alec's picture

PointList constructor

From time to time people ask on this site or elsewhere whether modules in Maple can be used the same way as objects in object oriented languages. The answer is yes and no. Yes - because OOP behavior can be simulated with modules - certainly not with full blown functionality, but still. No - because that is usually not the best way to do things in Maple. Here is an example of creating a type and an 'object' exploring it, with few 'methods'.

alec's picture

Whattype

Recently, working on Nested Verification, I took a look at the whattype procedure.

First, I couldn't find any use of local variables t1, t2, L, k1, k2 declared in it. That seems odd.

Second, the order of types in it seems to be not exactly right. In particular, symbol is located earlier than `module`. Also, some types are missing, record for example. That leads to not recognizing modules and records,

r := Record( 'a', 'b' ):
m := module() end:
whattype(r);
                                symbol
whattype(m);
                                symbol

Similarly to the compact and efficient version of Nested Verification, I wrote a "compact and efficient" version of whattype,

alec's picture

Nested Verification

Nested verification can be done in Maple using the following command,

VerifyTools:-AddVerification('nested'=proc(x,y,ver::verification)
if whattype(x)=whattype(y) then 
if x::Vector then verify(x,y,'Vector'('nested'(args[3..-1])))
elif x::Matrix then verify(x,y,'Matrix'('nested'(args[3..-1])))
elif x::Array then verify(x,y,'Array'('nested'(args[3..-1])))
elif x::array then verify(x,y,'array'('nested'(args[3..-1])))
elif x::set then verify(x,y,'set'('nested'(args[3..-1])))
elif x::list then verify(x,y,'list'('nested'(args[3..-1])))
elif x::relation then verify(x,y,'relation'('nested'(args[3..-1]))) 
elif x::range then verify(x,y,'range'('nested'(args[3..-1]))) 
else verify(x,y,args[3..-1]) fi 
else verify(x,y,args[3..-1])
fi end);

Here is a simple procedure doing numerical inverse Laplace transform using Post's inversion formula,

ILT:=proc(f,s) local k,dig;
if nargs>2 and type(args[-1],'posint') then dig:=args[-1] 
else dig:=Digits fi;
proc(T) local t,d,a;
d:=dig;
a:=Limit();
t:=evalf(T,dig);
while op(0,a)='Limit' do
a:=evalf(Limit((-1)^k/k!*(k/t)^(k+1)*eval(diff(f,s$k),s=k/t),k=infinity),d); 
d:=2*d od;
evalf(a,dig) end end:
alec's picture

1d phase portrait in Classic Maple

I wrote a procedure for drawing a 1d phase portrait of an equation

                        `x&prime;`=f(x)

in Classic Maple,

phase:=proc(f,r)
local a,b,bn,bs,U;
a:=op([1,1],plot(args));
bn:=-`-`(op([2,1..2],r));
bs:=[a[1,1],seq(`if`(
(a[i-1,2]-0.0001*bn*signum(a[i-1,2]))*(a[i,2]-0.0001*bn*signum(a[i,2]))<0,
`if`(a[i-1,2]*a[i,2]<0, (a[i-1,1]*a[i,2]-a[i-1,2]*a[i,1])/(a[i,2]-a[i-1,2]),
fsolve(f,subsop([2,1]=a[i,2]-0.02*bn,[2,2]=a[i,2]+0.02*bn,r))),NULL),
i=2..nops(a)),a[-1,1]];
b:=bs/bn;
U:=remove(x->abs(x[2,1])<0.04,
[seq([[b[i]+0.01,0],[b[i+1]-b[i]-0.02,0]],i=`if`(a[1,2]<0,2,1)..nops(b)-1,2),
seq([[b[i+1]-0.01,0],[b[i]-b[i+1]+0.02,0]],i=`if`(a[1,2]<0,1,2)..nops(b)-1,2)]);
plots[display](plots[arrow](
U,width=0.02,head_length=0.04,view=[b[1]..b[-1],-1..1],color=blue),
plots[pointplot](map(x->[x,0],b[2..-2]),symbol=circle,symbolsize=19),
plots[textplot](map(x->[x/bn,-0.07,sprintf("%.2f",x)],bs[2..-2])),
axes=none)
end:

It works as in the following example for f(x)=sin(x),

phase(sin(x),x=-8..8);

Syndicate content
}