Carl Love

Carl Love

28055 Reputation

25 Badges

12 years, 359 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@mmcdara Actually, there are two (unrelated) small things wrong with your method:

1. The support of your distribution is {1, ..., K+1} (as clearly seen from the horizontal axis tickmarks of your histogram) whereas the OP wanted {0, ..., K}. You can correct this by using EmpiricalDistribution instead of ProbabilityTable. Indeed, the support of a distribution created with ProbabilityTable is always an initial segment of positive integers (minus any values that have probability explicitly set to 0 in the table).

2. The values returned by Sample are floats rather than integers. This is a nuisance that can be corrected with trunc.

@eggoodaire Of course, it is always risky to use single-letter global names as labels for anything. If you want to free up for whatever use, do

interface(imaginaryunit= ...);

where you fill in whatever symbol for ....

But, I recommend that you use strings.

You may not be aware that edge weights can be included in the original graph definition, and that when drawn with option style= planar, the results are pretty good. So, your example could be

restart;
GG:= GraphTheory:-Graph({
   [{1,2}, 7], [{1,7}, 8], [{1,8}, 5], [{2,3}, 8], [{2,8}, 4], [{2,9}, 3],   [{3,4}, 8],  [{3,9}, 5], [{4,5}, 4], 
   [{4,10}, 3], [{5,6}, 6], [{6,7}, 7], [{6,10}, 10], [{7,8}, 5], [{7,9}, 2], [{9,10}, 7]
});
HH:= GraphTheory:-RelabelVertices(GG, ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]);
GraphTheory:-DrawGraph(HH, style= planar);

Or you can use your custom vertex positioning, but allow DrawGraph to decide on the edge-weight positioning:

vp := [[0,0], [5,10], [20,17], [50,20], [70,0], [50,-10], [20,-10], [18,2], [35,10], [55,5]];
GraphTheory:-SetVertexPositions(HH, vp);
GraphTheory:-DrawGraph(HH);

It's not perfect, but it's a lot less work than you figuring out the positioning of the edge labels.

@ecterrab Thank you for the detailed explanation. I was aware of the use of differentiating wrt a function in analyzing differential equations, for example, as it is used in the "uniqueness" part of the standard existence and uniqueness theorem for 1st-order IVPs. I conflated this desired functionality with the command Physics:-Fundiff when it is actually provided by Physics:-diff.

@primogen The specindex(theta) tells indets (and thus also subsindets) to search for the symbol theta with any index. Since d is a single instance rather than a set of instances, it could be handled simply with subs, like this:

qdot:= diff~(subsindets(subs(d= d(t), MassMatrix), specindex(theta), T-> T(t)),t)

However, I think that this is more robust:

qdot:= diff~(subsindets(MassMatrix, {identical(d), specindex(theta)}, T-> T(t)),t)

and this even more robust:

qdot:= diff~(subsindets(MassMatrix, {'identical'(d), 'specindex'(theta)}, T-> T(t)),t)

although I'll admit that typing the identical and those quotes is annoying.

@Rohith 

1: I can't reproduce that behavior. When I do termExtract("a-c*d"), I get output {c*d}. Nonetheless, I may be able fix the problem anyway if I can see the internal representation of the expression on your computer. So, please do

lprint(kernelopts(version), InertForm:-Parse("a-c*d"));

and show me the output. My output is

`Maple 2018.1, X86 64 WINDOWS, Jun 8 2018, Build ID 1321769`, `%+`(a, -`%*`(c, d))

I suspect that the last part of your output might be `%+`(a, `%*`(-c, d)). If it is, I'd like to know why; but even if I don't know, I may be able to fix it. As we've discussed from the beginning of this project, the minus sign is handled differently than other operators by InertForm:-Parse in that it is always treated as an unary operator.

2: I need for you to make a list of all the expressions that we've handled so far. I need that so that when I modify the program I can test to make sure that all previous correct behavior is maintained.

3: Regarding OP: It is just a parameter in the short arrow expression OP-> foldl(op(0,OP), op(OP)). As such, it is local to that expression, and both its spelling and its meaning are entirely up to the author, me. It has no connection to op. The same is true of any name appearing on the left side of an arrow ->. Since this arrow expression appears as the third argument of subsindets, the values passed to OP are determined by the second argument, AssocOps, which is defined a few lines above as `%+` or `%*` with three or more arguments.

Why do you ask about this symbol OP in particular?

 

@Rohith You have List2 and List1 reversed.

That seq(assign(...), ...could also be done as

assign(List1=~ List2);

However, I share Acer's disdain of using the assign command in the context of your Question.

@Ronan Names built with indexing, such as Pedal[i], serve the same purpose programmatically as Pedal||i, but they share the same lexical level (local, global, in-between) as their parent Pedal. Just don't try to assign to the parent (unless you really know what you're doing); only make assignments to the indexed forms. The indices can be anything, even multiple things; they don't need to be numbers or even names.

To delve a bit into techical details, assigning to A[i] where has not been previously assigned turns into a table, which you can look up help for. Nonetheless,  you can go pretty far using indexed variables without knowing this detail.

@acer Could you explain the differences between Physics:-Fundiff(..., f(t))Physics:-diff(..., f(t)), and perhaps also Physics:-Fundiff(e, f) where f(t) occurs in and "naked" f does not appear in e as an independent variable?

@primogen Given that Physics:-diff does what you want, I'm sorry that I led you astray about Physics:-Fundiff in the other thread. It's totally my fault. I don't have much knowledge of the Physics package.

@Rouben Rostamian  Your Answer corresponds closely to what I learned in PDEs classes in graduate school; although, at the time, I had great difficulty "swallowing" the deviation from standard (or perhaps I should say classical) mathematics. I believe, vaguely, that I was told that all can be made mathematically rigorous by replacing zeroes in certain places with constructions built up from

Z:= x-> piecewise(x=0, 0, exp(-1/x^2)),

a function that is very similar to 0 for real (all orders of its derivatives at equal 0). If you delve into the complex numbers, the arguments don't hold up---a fact that still unsettles me.

I can't add anything mathematically to the three Answers by PDE experts already given. I just want to point out some elementary issues with your syntax. Indeed, I was about to say that it was impossible to unambiguously interpret what you typed, but three experts have interpreted it the same way, so I guess that it's possible. However, neither Maple nor any other mathematical software that I am aware of will see it that way.

1. All functions require parentheses to separate the function's name from its arguments, even if those parentheses wouldn't be used in standard typeset mathematical printing. So, you need cos(y)sin(y)sin(x)sinh(Pi), etc.

2. Uppercase and lowercase letters are distinct symbols, with no inherent connection in meaning. In this case, most human readers, including me, can see that your U and u mean the same thing. However, Maple will not see it that way.

3. Specific to Maple: Follow Rouben's example on how to enter derivatives of unknown functions: diff(u(x,y), x, x), etc.

@Rohith Try this code. There are only two differences: 

1. I commented out the ModuleUnload procedure, which is the procedure that removes the added types when the module gets garbage  collected. My guess is that that garbage collection is happening at inappropriate times due to a bug in Maple 2018.

2. I added protection for Expression so that it can't inadvertently be globally assigned to.

termExtract:= module()
uses TT= TypeTools, IN= InertForm;
local
   TryRemoveType:= (T::name)-> if TT:-Exists(T) then TT:-RemoveType(T) else fi,
   TryAddType:= (TH::(name= anything))-> (TryRemoveType(lhs(TH)), TT:-AddType(op(TH))),
   AddedTypes,
   (* Removed 23-Oct-2018 because types were mysteriously missing.
   ModuleUnload:= proc()
      (TryRemoveType@lhs)~(AddedTypes);
      :-`print/Expression`:= ':-`print/Expression`';
      NULL
   end proc,
   *)
   ModuleLoad:= proc()
      protect(Expression);
      TryAddType~(
         (AddedTypes:= [
            #With recursive types, put base cases at beginning to avoid infinite loops!
            'Atomic'=     'Or'('name', 'numeric', 'InertRecip', 'Negated'),
            'InertRecip'= 1 %/ 'Atomic',
            'Negated'=    (-1) &* 'Atomic',
            'AssocOps'=   'And'('specfunc'({`%+`, `%*`}), 'patfunc'('anything'$4)),
            'Operators'=  'specfunc'('Atomic', {`%+`, `%*`, `%/`, `%^`}),
            'Expression'=     'And'('Operators', 'Not'('InertRecip'), 2 &under nops)
         ])
      );
      :-`print/Expression`:= Expression-> value({args});
      NULL
   end proc,

   #Associative operators with > 2 operands are regrouped pairwise.
   Fold:= E-> subsindets(E, AssocOps, OP-> foldl(op(0,OP), op(OP))),
 
   ModuleApply:= (E::string)-> 'Expression'(indets(Fold(IN:-Parse(E)), 'Expression')[])
;
   ModuleLoad()
end module:

 

Since your equation doesn't contain an undifferentiated y(x), it is equivalent to this first-order equation:

diff(Y(x),x)+(A/x+B/x^2+C)*Y(x)+D4*Y(x)^2/x+E*Y(x)^3+F/x+G/x^2+H/x^3 = 0

where Y(x) = diff(y(x),x) and D4= D. (I changed your to D4 to avoid conflict with the D, Maple's differential operator.) This simplification doesn't make dsolve able to solve the reduced equation symbolically, but you certainly can't call it "high order" anymore.

Would you be interested in a purely numeric solution?

@sand15 Since the OP said "discret uniform distribution", I interpreted the function as 1/(k+1), which I thought was the only reasonable interpretation under those circumstances. Since my code didn't directly use the function, I didn't comment on the typo (just as I didn't comment on "discrete" being misspelled "discret").

The method that you presented in your Answer is both correct and useful for arbitrary discrete distributions[*1]. I have no problem with that. 

The point of my Answer is that for the specific and very common case of a discrete uniform distribution whose support is a range of integers, it is easy and the usual Maple coding practice to generate samples simply by using rand[*2]. It may be possible to improve the efficiency a little by instead using RandomTools:-Generate. I think that the efficiency is reduced by using ProbabilityTable and Statistics:-Sample; however, I haven't tested that in Maple 2018, which may have introduced a substantial efficiency improvement for Sample used with custom distributions.

[*1]Actually, I did just notice a problem with your method, which I just put as a Reply to your Answer.

[*2]It is not as widely known as it should be that since the output of rand(m..n) for numeric m and n is a procedure rather than the random number itself, the following is an inefficient way to generate a sample of size N because it recreates that procedure for each number generated:
seq(rand(0...k)(), i= 1..N).
I didn't comment on this issue in my Answer (although I probably should have). Although my code handles this issue efficiently, it does so at the expense of some perhaps-unexpected complexity of the code.

@vv Those are great questions, and I was just about to ask similar questions myself, in a separate Question, as I just came up with the code in question today. The documentation of thismodule is very thin, and what I know is by expermentation. Surely its relationship to its parent module is much more complex than thisproc's relationship to its parent procedure; and surely its relationship to the :- and [] operators is more complex than other modules' relationship to those operators. And it usually can't be replaced by the named module that it refers to.

@shimaa sadk 

There is a simpler way that I didn't see before:

#Remove n randomly selected elements from list L:
RemoveRand:= (L::list, n::nonnegint)-> combinat:-randcomb(L, nops(L)-n); 

And here's an example of its use:

#Construct a random list:
R:= rand(-99..99):
L9:= sort(['R()'$9]);

        L9 := [-83, -82, -73, -44, -10, -4, 62, 80, 97]

#Remove 3 randomly chosen elements from it:
L6:= RemoveRand(L9, 3);

               L6 := [-83, -44, -10, -4, 62, 80]

 

First 299 300 301 302 303 304 305 Last Page 301 of 709