mmcdara

6149 Reputation

17 Badges

9 years, 73 days

MaplePrimes Activity


These are replies submitted by mmcdara

@vv 

I see two problems in your worksheet.

1. The distribution corresponding to the RV MIXTURE is not correctly defined. It should be:

pdf := (p,x) -> p*PDF(U1, x)*Heaviside(x+3) + (1-p)*PDF(U2, x)*Heaviside(x-1);
cdf := (p,x) -> int(pdf(p,y), y=-infinity..x);


Right, this was my first attempt but I got an error withe the MIXTURE := p -> RandomVariable(...)  instruction.
I just copy-paste your code and I can't reproduce this error: probably I'd done some typo but the fact is, I came to use this "incorrect" definition of pdf and cdf for it did not return an error.

In any case your correction partially solves my problem (it remains the sampling of the whole "support")

Thank you very much

 

PS : I don't know, me neither, how to define the support of a rv. Just as I don't know how to define a custom sampler: the help page is not very instructive on this point.

 

@Carl Love @vv

Thanks to both of you.

Could I infer from what you said that "arithmetic of infinity" is not completely (could I say "without flaws'?) implemented in Maple and that, in all cas, we must be very carefull while using it?

 

@Kitonum 

In a sense, this is natural, because legend option is associated with the image of curves..." it is what I suspected too.

I'm at home right now with my old Maple 2015 version: in my reply to Carl Love you will see that the error message is very explicit with this version, I do not remember thios is still the case in Maple 2019.

Thanks for the help

@Carl Love 

here it is
 

restart:

Unfortunately I'm home right now and I still use Maple 2015.

The error message is so clear that receiving it with Maple 2019  would not have lead me to ask a question.

interface(version)

`Standard Worksheet Interface, Maple 2015.2, Mac OS X, December 21 2015 Build ID 1097895`

(1)

plots:-inequal({y > t, y < t^2}, y=-2..2, t=-2..2, legend="test")

Error, (in plots:-display) display does not accept the legend option

 

 


 

Download inequal.mw

@acer 

It works perfectly.
Thanks for the help

@Kitonum 

Why not just write the following 2 lines and it will be beautiful:... because the "structure"  I want to display doesn't have a matrix shape.

An example of this "structure" is given in this help page DocumentTools:-Layout:-Cell :

T := Table(Column(),Column(),Column(),widthmode=pixels,width=90,alignment=center,
           Row( Cell( "A1", columnspan=2 ), Cell("A2", rowspan=2 ) ),
           Row( Cell( "B1", rowspan=2), Cell( "B2" ) ),
           Row( Cell( "C", columnspan=2 ) )):
InsertContent(Worksheet(Group(Input( T )))):

 

... and the cell B1, for instance, must contain the piecewise relation : so Tabulate is useless here

@Carl Love 

Thank you very much, your answer suits me perfectly and the polynomial case is more than enough.
(PS: I wasn't aware about type recursivity).

The  footnote and your conclusion are very clear.

@Carl Love @acer  @Joe Riel

Thank you three for your precious help.

Here is a special comment on Carl's sentence "But mostly I want to draw your attention to the distinction between types and properties because I think that you may be referring to the latter".

 I know a little bit about assume and properties and I think that my question was reallyoriented on types (even if it was perhaps not clear enough). 

Maybe you will have a better understanding of my question if I explain its context.
In one of your replies, months ago, you showed me how to define a type 'RV' (for Random Variable) while using TypeTools:-AddType.
I'm presently working on a course on Statistics and I decided to use Maple to explain some concepts.
The minimal way to define a random variable with package Statistics is 

dist := Distribution(PDF=(t->f(t))):
V := RandomVariable(dist);


And I thought it would have been even better if I could write something like V::RV
where type RV is (your code) :
TypeTools:-AddType(
     RV, 
     {RandomVariable, 
       'RandomVariable^posint', 
        '`*`'({RandomVariable, 'RandomVariable^posint'})
     }
);


Related to this point is the following :

  • Suppose you define 2 rvs X and Y (Statistics:-RandomVariable)
    You can verify that [attributes(X)][2] is RandomVariable (same thing for Y)
  • Now set Z=X+Y ; Z is obviously a random variable, but Z has no attributes and is not the same "type of object" than X or Y.
  • I tought that if it was possible to declare that both X and Y where of some type (RV) then this type could be inhereted by Z ..???

@Samir Khan 

Received five out of five, I took the opportunity to vote up here

@Samir Khan 

Thanks, I'm going to look at it

Nice !
I've seen these types of plots from parameter curves before and I've always wondered how to get the expressions of these curves.
Could you briefly explain to me how you did it?

Happy new year

Fine, 

What is the minimum version of Maple which supports the 'rotation' option in textplot ?

A classical way to compute this integral is to use a serie expansion of the integrand :
 

restart:

J0 := Int(cosh(cos(2*t)), t = 0 .. 2*Pi)

Int(cosh(cos(2*t)), t = 0 .. 2*Pi)

(1)

J1 := IntegrationTools:-Change(J0, t=u/2)

(1/2)*(Int(cosh(cos(u)), u = 0 .. 4*Pi))

(2)

J2 := map(simplify, convert(J1, exp))

(1/4)*(Int(exp(cos(u))+exp(-cos(u)), u = 0 .. 4*Pi))

(3)

# A classical way to compute this integral is to use a serie expansion of the integrand
# formally (N --> + infinity):

L1 := N -> 1/4 * int(add(cos(u)^n/n!, n=0..N), u=0..4*Pi)

proc (N) options operator, arrow; (1/4)*(int(add(cos(u)^n/factorial(n), n = 0 .. N), u = 0 .. 4*Pi)) end proc

(4)

eval(L1(100) + subs(cos(u) = -cos(u), L1(100)));

(494862290230313779810714536331763867748886021312314850235382520276354031282881032749106453076199887040428321869725955993930836848899840675596580259521486096667/195433073004454404037640198313320859318965533267125423756511234573420484197258217148892374914594467610273916848620365158942916187521024000000000000000000000000)*Pi

(5)

evalf[20](%);

7.9549265210128452746

(6)

# speed of convergence

phi := N -> eval(L1(N) + subs(cos(u) = -cos(u), L1(N))):

vals := [ seq([N, phi(N)], N in [seq(1..41, 2)]) ]:
increments := [ seq([N, log[10](vals[N+1][2]-vals[N][2])], N=1..numelems(vals)-1)]:
plot(increments, gridlines=true)

 

 


 

Download Quasi_exact_solution.mw

 

@vv 

I do not completely disagree with you :

  • Your first proposal is awkward because 'diff(f(a+b, a-b), x__2, x__1)' returns    but 
    diff(f(a+b, a-b), x__2, x__1) returns 0... hence an ambiguity in the writing of  : does it means 0 or not?
     
  •   seems reasonable, even if some previous explanation of the meaning of f[1, 2] ought to be necessary (even if all mathematicicians will probably understand it first glance).
     
  •  doesn't satisfy me, it is very close to D[1, 2] which is a little bit abstract (in fact I often faced people that do not have particular knowledge of Maple and that are put off withe this D[...] notation).


The point I completely agree with you is that the Liebniz notation, while very clear, is too verbose and difficult to read whebn applied to complex expressions like the one I presented.

Another possibility (close to your proposal, could be  (\partial_{1, 2]f)(a+b, a-b) (in LaTeX notation).


To conclude, a thing I'm sure of is that the default notation (Maple <= 2016) seems awfull to me.
On the example below, I'm quite satisfied with the notation D[1]f (maybe  (D[1]f) would suit me better), 
but not by what it becomes after using convert(..., Diff).

restart;

T := mtaylor(f(U__1, U__2), [U__1=mu__1, U__2=mu__2], 2);

f(mu__1, mu__2)+(D[1](f))(mu__1, mu__2)*(U__1-mu__1)+(D[2](f))(mu__1, mu__2)*(U__2-mu__2)

(1)

M := convert(T, Diff)

f(mu__1, mu__2)+(Diff(f(mu__1, mu__2), mu__1))*(U__1-mu__1)+(Diff(f(mu__1, mu__2), mu__2))*(U__2-mu__2)

(2)

 


 

Download Awfull.mw

 

I've written this piece of code xhich seems to work correctly.
Nevertheless I'm not one hundred percent sure it still works correctly for other expressions of the same kind.
Probably this could be written more efficiently...


 

restart


The black code above comes from this Mapleprimes' Post
(personal modifications are pink highlighted)

with(DocumentTools):

with(DocumentTools:-Layout):

F := Font("Liebniz notation", size=12, color=blue, style=:-Hyperlink):

H := DocumentTools:-Layout:-Hyperlink(F,linktarget="https://www.mapleprimes.com/maplesoftblog/210289-Writing-Derivatives-At-A-Point-Using"):

InsertContent(Worksheet(Group(Input(Textfield("Original post from Technical Support ",H))))):

 

# Type to check if an expression is a derivative using 'D', e.g. D(f)(a) and D[1,2](f)(a,b).

 

TypeTools:-AddType(   

 

        'Dexpr',      

 

        proc( f )     

 

               if op( [0,0], f ) <> D and op( [0,0,0], f ) <> D then

 

                       return false;

 

               end if;       

 

               if not type( op( [0,1], f ), 'name' ) or not type( { op( f ) }, 'set(algebraic)' ) then

 

                       return false;

 

               end if;       

 

               if op( [0,0,0], f ) = D and not type( { op( [0,0,..], f ) }, 'set(posint)' ) then

 

                       return false;

 

               end if;       

 

               return true;          

 

        end proc      

 

):

 

 

# Create a local version of 'print', which will print expressions like D[1,2](f)(a,b) in a custom way,

 

# but otherwise print in the usual fashion.

 

Dprint := proc()

 

 

        local A, B, f, g, L, X, Y, Z;

 

 

        # Check that a valid expression involving 'D' is passed, along with a variable name or list of variable names.

 

        if ( _npassed < 2 ) or ( not _passed[1] :: 'Dexpr' ) or ( not passed[2] :: 'Or'('name','list'('name')) ) then

 

               #return :-print( _passed );

               return  _passed;

 

        end if;

 

 

        # Extract important variables from the input.

 

        g := _passed[1]; # expression

 

        X := _passed[2]; # variable name(s)

 

        f := op( [0,1], g ); # function name in expression

 

        A := op( g ); # point(s) of evaluation

 

 

        # Check that the number of variables is the same as the number of evaluation points.

 

        if nops( X ) <> nops( [A] ) then

 

               #return :-print( _passed );

               return  _passed;

        

        end if;

 

 

        # The differential operator.

 

        L := op( [0,0], g );

 

 

        # Find the variable (univariate) or indices (multivariate) for the derivative(s).

 

        B := `if`( L = D, X, [ op( L ) ] );

 

 

        # Variable name(s) as expression sequence.

 

        Y := op( X );

 

 

        # Check that the point(s) of evaluation is/are distinct from the variable name(s).

 

        if numelems( {Y} intersect {A} ) > 0 then

 

               #return :-print( _passed );

               return  _passed;

        

        end if;

 

 

        # Find the expression sequence of the variable names.

 

        Z := `if`( L = D, X, X[B] );

 

        #return print( Eval( Diff( f(Y), Z ), (Y) = (A) ) );

        return Eval( Diff( f(Y), Z ), (Y) = (A) );

 

 

 

end proc:

# Example

expr := -(Diff(f(mu__1, mu__2), mu__1))^2*mu__1^2-(Diff(f(mu__1, mu__2), mu__2))^2*mu__2^2+2*(Diff(f(mu__1, mu__2), mu__1))^2*lambda__1^2+2*(Diff(f(mu__1, mu__2), mu__2))^2*lambda__2^2+2*(Diff(f(mu__1, mu__2), mu__1))*(Diff(f(mu__1, mu__2), mu__2))*lambda__1*lambda__2-2*(Diff(f(mu__1, mu__2), mu__1))*mu__1*(Diff(f(mu__1, mu__2), mu__2))*mu__2 + anything_else;

-(Diff(f(mu__1, mu__2), mu__1))^2*mu__1^2-(Diff(f(mu__1, mu__2), mu__2))^2*mu__2^2+2*(Diff(f(mu__1, mu__2), mu__1))^2*lambda__1^2+2*(Diff(f(mu__1, mu__2), mu__2))^2*lambda__2^2+2*(Diff(f(mu__1, mu__2), mu__1))*(Diff(f(mu__1, mu__2), mu__2))*lambda__1*lambda__2-2*(Diff(f(mu__1, mu__2), mu__1))*mu__1*(Diff(f(mu__1, mu__2), mu__2))*mu__2+anything_else

(1)

LiebnizNotation := proc(expr, vars)
  local DiffTerms, Remainder, opDiffTerms, S, A, u, v:
  DiffTerms   := map(u -> if has(u, 'Diff') then u end if, [op(expr)]):
  Remainder   := expr-add(DiffTerms);
  opDiffTerms := map(u -> [op(u)], DiffTerms):
  S := 0:
  for u in opDiffTerms do
    A := 1:
    for v in u do
      if has(v, 'Diff') then
        patmatch(v, (d::algebraic)^n::integer,'la');
        A := A * Dprint(convert(eval(d, la), 'D'), [x__1, x__2])^eval(n, la);
      else
        A := A*v
      end if:
    end do:
    S := S+A:
  end do:
  return S + Remainder
end proc:

LiebnizNotation(expr, [u__1, u__2])

-(Eval(Diff(f(x__1, x__2), [x__1]), (x__1, x__2) = (mu__1, mu__2)))^2*mu__1^2-(Eval(Diff(f(x__1, x__2), [x__2]), (x__1, x__2) = (mu__1, mu__2)))^2*mu__2^2+2*(Eval(Diff(f(x__1, x__2), [x__1]), (x__1, x__2) = (mu__1, mu__2)))^2*lambda__1^2+2*(Eval(Diff(f(x__1, x__2), [x__2]), (x__1, x__2) = (mu__1, mu__2)))^2*lambda__2^2+2*(Eval(Diff(f(x__1, x__2), [x__1]), (x__1, x__2) = (mu__1, mu__2)))*(Eval(Diff(f(x__1, x__2), [x__2]), (x__1, x__2) = (mu__1, mu__2)))*lambda__1*lambda__2-2*(Eval(Diff(f(x__1, x__2), [x__1]), (x__1, x__2) = (mu__1, mu__2)))*mu__1*(Eval(Diff(f(x__1, x__2), [x__2]), (x__1, x__2) = (mu__1, mu__2)))*mu__2+anything_else

(2)

 

 


 

Download Liebniz_Notation.mw

First 85 86 87 88 89 90 91 Last Page 87 of 125