mmcdara

7891 Reputation

22 Badges

9 years, 54 days

MaplePrimes Activity


These are questions asked by mmcdara

Hi, 

I found that using the packages heometry and simplex in the same worksheet (only one restart at the top of it) causes a conflict in the command simplex:-convexhull(`some set of points`, option = area).

It seems that Maple doesn't confounds  that area in the option=area  with geometry:-area
Is it possible to fix this?


 

restart:

L := { [0,0], [1/2,1/2], [1,1], [2,0], [1,0], [1,1/2] };
simplex:-convexhull(L, 'output'='area');

{[0, 0], [1, 0], [1, 1], [1, 1/2], [2, 0], [1/2, 1/2]}

 

1

(1)



Use of package geometry
 

restart

with(geometry):

triangle(T, [point(A,0,0), point(B,2,0), point(C,1,3)]):  # just an example

L := { [0,0], [1/2,1/2], [1,1], [2,0], [1,0], [1,1/2] };
simplex:-convexhull(L);
simplex:-convexhull(L, 'output'='area');

{[0, 0], [1, 0], [1, 1], [1, 1/2], [2, 0], [1/2, 1/2]}

 

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

 

Error, invalid input: simplex:-convexhull expects its 2nd argument, opts, to be of type identical(output) = {list({identical(area, hull, plot)}), identical(area, hull, plot)}, but received output = geometry:-area

 



To avoid this conflict between 'output'='area' and geometry:-area I did this
 

restart

geometry:-triangle(T, [point(A,0,0), point(B,2,0), point(C,1,3)]):

triangle: 0 < -point(A 0 0)+point(B 2 0)+point(C 1 3)

Error, (in geometry:-triangle) not enough information: the triangle is not defined

 

L := { [0,0], [1/2,1/2], [1,1], [2,0], [1,0], [1,1/2] };
simplex:-convexhull(L);
simplex:-convexhull(L, 'output'='area');

{[0, 0], [1, 0], [1, 1], [1, 1/2], [2, 0], [1/2, 1/2]}

 

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

 

1

(2)

 


 

Download geometry_vs_simplex.mw

Hi, 

Versions concerned:  [ Maple 2015 ... Maple 2018 ]

I use DocumentTools:-Tabulate to display a matrix of numbers while coloring them according to some condition.
(line DocumentTools:-Tabulate(M, color=((M,i,j)->`if`(M[i,j]>3,....) below ... please note the output is not loaded for some unknown reason).
The fact is that the matrix appears with black characters meaning 'color' doesn't work.

In a second attempt I convert matrix M into a matrix of strings and use now
DocumentTools:-Tabulate(S, color=((S,i,j)->`if`(parse(S[i,j])>3,...)
I get now the desired result with some blue and red numbers.

So converting to strings could be a workaround.
But think to matrices where elements would be algebraic expressions, for instance 
M := Matrix(2, 2, (i,j)->exp(x^i)+cos(x*j))
and that we use the coloring scheme is color=((M,i,j)->`if`(i+j>3, "Red", "Blue")
Converting M to a string matrix will display the element [2, 2] in red and the others in blue, but what you get then is a no longer a 2D pretty output but, literally, things like exp(x^2)+cos(x*2) 

The "convert to string" workaround is thus far from perfect.
Is the fact that 'color' only acts on strings a "normal and known" behaviour?
Is it possible to change the color of the font for non "string type matrices" ?
 

restart:

M :=Matrix(2, 2, (i,j)->i+j)

M := Matrix(2, 2, {(1, 1) = 2, (1, 2) = 3, (2, 1) = 3, (2, 2) = 4})

(1)

DocumentTools:-Tabulate(M, color=((M,i,j)->`if`(M[i,j]>3, "Red", "Blue")), width=30)

S :=convert~(M, string):
DocumentTools:-Tabulate(S, color=((S,i,j)->`if`(parse(S[i,j])>3, "Red", "Blue")), width=30)

 


 

Download Tabulate_Color.mw

Hi, 
I'm very stunned by the results displayed in the worksheet below.
It seems that adding extra assumptions on a variable already constrained with "assume" has disturbing side effects ?

Here is a simplified worksheet that exhibits the phenomenon.
Basically I want to solve the equation a*x-b =b with respect to x.
In a first step I set assumptions on a and b and Maple returns x=0, as expected.
Next I add a new condition on b and ask Maple to solve the equation again. I intain this strange result: x=(b~ - b~)/a~.

Am I doing something wrong?

Thanks in advance


 

restart:

anames(user);

assume(a > 0):
assume(b > 0):

f := a*x+b

a*x+b

(1)

g := f=b:
simplify(g);
solve(g, a);

a*x+b = b

 

Warning, solve may be ignoring assumptions on the input variables.

 

0

(2)

anames(user);

b, g, f, a

(3)

additionally(b > a)

h := f=b:
simplify(h);
solve(h, a);

a*x+b = b

 

Warning, solve may be ignoring assumptions on the input variables.

 

anames(user)  # additionally(b > a) doesn(t introduce a new variable

b, h, g, f, a

(4)

indets(g);
indets(h);  # why do I have b~ twice ?
            # This is probably the reason why solve(h, a) doesn't work

{a, b, x}

 

{a, b, b, x}

(5)

lprint(indets(h))

{a, b, b, x}

 

# strange...

solve(h, indets(h)[1]);

simplify(%);

Warning, solve may be ignoring assumptions on the input variables.

 

-(b-b)/x

 

-(b-b)/x

(6)

# remark, dismantle explains nothing about the distinction between b~ and b~


EQUATION(3)
   POLY(6)
      EXPSEQ(4)
         NAME(4): a
         NAME(4): b
         NAME(4): x
      DEGREES(HW): ^2 ^1 ^0 ^1
      INTPOS(2): 1
      DEGREES(HW): ^1 ^0 ^1 ^0
      INTPOS(2): 1
   NAME(4): b
 

 

dismantle(h)


EQUATION(3)
   POLY(6)
      EXPSEQ(4)
         NAME(4): a
         NAME(4): b
         NAME(4): x
      DEGREES(HW): ^2 ^1 ^0 ^1
      INTPOS(2): 1
      DEGREES(HW): ^1 ^0 ^1 ^0
      INTPOS(2): 1
   NAME(4): b
 

 
 

 


 

Download MultipleAssumptions.mw

 

Hi, 

In two lines below Command1 and Command2 have the same value

Command1 := subs([a=1, b=2], a*x+b);
Command2 := eval(a*x+b, [a=1, b=2]);

Are there pros and cons of using one over the other?

Thanks in advance

Hi, 

Here's a problem that's been bothering me for a long time:

The Statistics[Distribution] help page describes the way to construct one's own Random Variable (RV).
All the examples construct a distribution foo := Distribution(...) next used to define the RV X by typing
X := RandomVariable(foo)

On the other way, constructing a RV Y from predefined Maple's distributions writes 
Y := RandomVariable(bar(sequence of parameters))

The "user defined RV feature" (MyRV := RandomVariable(foo)) seems to be restricted to distributions where all the parameters are numerically instanciated, contrary to "Maple's defined distributions" where these parameters can be kept formal).
So my question: Is it possible to define your own Distribution (module foo) in such a way that the definition of the RV X writes
X := RandomVariable(foo(sequence of parameters))

The attached file contains an example for the Levy distribution (the second part is just a workaround where a substitution is used to balance my inability to define a RV the way above)


 

restart:

with(Statistics):

A particular Levy distribution

Levy Distribution  Levy(m=0, s=1)
m : location parameter
s := dispersion parameter

m := 0:
s := 1:

pdf := unapply(piecewise(x<0, 0, sqrt(s/2/Pi)*exp(-s/2/(x-m))/(x-m)^(3/2)), x);
cdf := unapply(erfc(sqrt(s/2/(x-m))), x);


samplingMethod := proc(N)
                     m +~ s /~ ( Quantile~(Normal(0, 1), Sample(Uniform(1/2, 1), N)) )^~2
                  end proc;

proc (x) options operator, arrow; piecewise(x < 0, 0, (1/2)*2^(1/2)*exp(-(1/2)/x)/(Pi^(1/2)*x^(3/2))) end proc

 

proc (x) options operator, arrow; erfc((1/2)*2^(1/2)*(1/x)^(1/2)) end proc

 

proc (N) `~`[:-`+`](m, ` $`, `~`[:-`/`](s, ` $`, `~`[:-`^`](`~`[Statistics:-Quantile](Normal(0, 1), Statistics:-Sample(Uniform(1/2, 1), N)), ` $`, 2))) end proc

(1)

Levy := Distribution(
                      PDF=(x->pdf(x)),
                      CDF= (x->cdf(x)),
                      RandomSample = (N->samplingMethod(N))
                    );

_m4561537536

(2)

X := RandomVariable(Levy):
PDF(X, x);
Sample(X, 5);

piecewise(x < 0, 0, (1/2)*sqrt(2)*exp(-1/(2*x))/(sqrt(Pi)*x^(3/2)))

 

Vector[row]([.569870365734461, .357020836498979, 39.1445163385190, .340597592385078, 1.23209867072410])

(3)


How should I define a formal Levy distribution in such a way that, as for any predefined RV in Maple,
a particular instance is defined by  X := RandomVariable(Levy(M, S))  
where M and S are numerical values?

m := 'm':
s := 's':

pdf := unapply(piecewise(x<0, 0, sqrt(s/2/Pi)*exp(-s/2/(x-m))/(x-m)^(3/2)), x);
cdf := unapply(erfc(sqrt(s/2/(x-m))), x);
samplingMethod := proc(N)
                     m +~ s /~ ( Quantile~(Normal(0, 1), Sample(Uniform(1/2, 1), N)) )^~2
                  end proc;

proc (x) options operator, arrow; piecewise(x < 0, 0, (1/2)*2^(1/2)*(s/Pi)^(1/2)*exp(-(1/2)*s/(x-m))/(x-m)^(3/2)) end proc

 

proc (x) options operator, arrow; erfc((1/2)*2^(1/2)*(s/(x-m))^(1/2)) end proc

 

proc (N) `~`[:-`+`](m, ` $`, `~`[:-`/`](s, ` $`, `~`[:-`^`](`~`[Statistics:-Quantile](Normal(0, 1), Statistics:-Sample(Uniform(1/2, 1), N)), ` $`, 2))) end proc

(4)

Levy := Distribution(
                      PDF=(x->pdf(x)),
                      CDF= (x->cdf(x)),
                      RandomSample = (N->samplingMethod(N))
                    ):

X := RandomVariable(Levy):
PDF(X, x);
print();

# here is an inelegant workaround

ms := {m=0, s=1};
subs(ms, PDF(X, x));
 

piecewise(x < 0, 0, (1/2)*sqrt(2)*sqrt(s/Pi)*exp(-(1/2)*s/(x-m))/(x-m)^(3/2))

 

NULL

 

ms := {m = 0, s = 1}

 

piecewise(x < 0, 0, (1/2)*2^(1/2)*(1/Pi)^(1/2)*exp(-(1/2)/x)/x^(3/2))

(5)

u := Sample(X, 2);

# The substitution doesn't work

subs(ms, u);

u := Vector[row](2, {(1) = m+HFloat(1.7545213270590625)*s, (2) = m+HFloat(0.332342758403606)*s})

 

Vector[row]([m+1.75452132705906*s, m+.332342758403606*s])

(6)

# just a copy-paste of lprint(u): the substitution does work


lprint(u);

subs(ms, Vector[row](2, {1 = m+HFloat(2.49542958430755446)*s, 2 = m+HFloat(4.80772083491271562)*s}, datatype = anything, storage = rectangular, order = Fortran_order, shape = []))

Vector[row](2, {1 = m+HFloat(.529024707466874800)*s, 2 = m+HFloat(1.50334452451578438)*s}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

 

Vector[row]([2.49542958430755, 4.80772083491272])

(7)

 


 

Download My-own-random-variable.mw

First 39 40 41 42 43 44 45 Page 41 of 48