Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are replies submitted by Carl Love

@mmcdara I think that the OP is trying to create a random variable (let's call it S) that is a sum of binomial random variables R[j] where the 'n' parameter of each R[k] is some function (let's call if f) based on random draws from R[j] $ j= 0..k-1. After S is created, they want a random draw or draws from it. I just can't figure out what f is, precisely.

@peter2108 Great, I'll convert it to an Answer then.

@peter2108 I think that a .mw worksheet would work better than a .maple file. Is that a possibility for you?

Could you just say in words precisely what you're trying to do? I can't entirely figure it out from your code. What is A[1] supposed to be? Is it supposed to be a random draw from the first distribution? If so, what's supposed to happen if the sum of the A[i] grows larger than N-n?

@shimaa sadk Would you please post your code, or a simplified example from it? It's not entirely clear whether you're talking about a list, a Vector, or a table. I know that you said "list", but it's very easy for a new user to mix those three types up, and my answer would be different for each. It could even be that you're starting with a list and inadvertently turning it into a table.

That being said, if L is a list, then L[1] is its first (and perhaps only) member. So, if x[i] is a list, then x[i][1] is the member.

@Adam Ledger The command kernelopts(level) will tell you how deep your "stack" is, that is, how deeply nested you are, whether that's through recursion or simply through A calls B calls C, etc. But mere depth is not the whole problem. If you go too deep, you get the relatively benign "stack limit exceeded" error. I think that a more-important factor may be breadth of recursion. Does recursive procedure R spawn one copy of itself, or two or more? Consider the naive way of computing Fibonaci numbers without a remember table:

F:= n-> F(n-1)+F(n-2): F(0):= 0: F(1):= 1: F(100)

I've had more trouble with recursive user-defined types than with straightforward procedures, especially when they're used for declared type restrictions on local variables and kernelopts(assertlevel) is set to 2.

An extremely informative command related to the level is debugopts(callstack). This'll tell you exactly how you got to the level that you're at: what procedures were called, and what were their arguments. Apparently, you can cause some havoc with this command according to its help page. I've never had trouble, but save your work if you're worried. The worst that can happen is that you'll crash the kernel again and you'll learn something.

When I'm trying to track down a difficult deeply nested error, I put the offending code in a try catch statement and put

    lprint(debugopts(callstack));
    error

in the catch block.

@Jjjones98 The nodes X[k] and weights C[k] depend on the number of evaluation points n, although they do not depend on the integrand. It's not just adding one more point; the nodes and weights change completely when you go from n=4 to n=5 (*footnote). So call my procedure NodesAndWeights for n=5 also.

In Maple, what you have as $\pi$ is Pi (uppercase P, lowercase i). So the function f becomes

f:= x-> cos(Pi*sin(x));

By default, Maple uses 10 digits of precision in its internal decimal computations. (Computations with non-decimal numbers are done exactly, always!) This precision is controlled by setting the Digits environment variable. Since it looks like your final goal has 10 digits, I recommend that you set Digits higher. At the start of the worksheet, do

Digits:= 15;

I didn't just pick 15 arbitrarily. In some senses it is the most efficient setting for Digits due to built-in features of nearly all computer chips. Here I'm accounting efficiency as roughly  -log(|error|) / (time usage * memory usage). Due to extreme optimizations done by chip and OS designers over the decades, the time and memory usage are essentially constant for all values of Digits <= 15 (and Digits is roughly -log10(|error|)).

*footnote: In some cases, the nodes for n = 2*N+1 will reuse all the nodes from n = N. The weights will still be different. These cases are extremely useful, and are called Gauss-Kronrod quadrature. See the Wikipedia article "Gauss-Kronrod quadrature formula".

FWIW, repeating the call Test(), or simply calling Coordinates(X, quiet) twice in succession inside Test produces the correct result. I have no idea why. This is not meant to be an adequate solution to your problem, nor have I marked it as an Answer. I'm merely putting it out there as another clue to the solution.

By setting printlevel to a high level (I used 99), you can see that thousands upon thousands of different things happen (like setting global variables) when Physics is loaded, whether from within the procedure or through with at the top level. Perhaps one of those things happens differently.

@tomleslie What you say doesn't apply to the "Kernel connection has been lost" error (which should be called the "kernel has crashed" error because that's what's happened in the vast, vast majority of cases that you get this message).

Regarding your first point: My experience with this error is that it's usually caused by doing things that are allowed by the documentation but are too complicated by being too heavily nested, too recursive, too many variables, etc.

Regarding your second point: This error can't be trapped. The kernel process has died, and only the interface process (and possibly kernels associated with other worksheets) remain alive.

That being said, and having read all the code that Adam has posted, I'd say that his code tends towards things being too heavily nested.

The set of functions R^[a,b] (i.e., the functions from interval [a,b] to the reals) is uncountable, so surely there can be no universal decision algorithm to decide which of them are integrable (and, the definition of "integrable" must be stated, as there are several competing definitions). This is not even addressing the issue of parameters, which vastly complicate the problem.

So,

  1. Please give two examples of the type of functions that you actually care about: one which is integrable and one which isn't
  2. Please state the type of integrability that you're using (Lebesgue, etc.)
  3. Please define "dimension" as you used it in the phrase "parameters (like dimension)".

@vv

Thanks for posting this compilable code.

I see that you used type(n,even). Surely the types that one can use with the highly symbolic type command are extremely limited in compiled Maple. Are those allowed types listed somewhere?

I see that you worked around the negativity problem that I had with endogenous frac by adding 5 to the result before doing frac. It tooked me several minutes to figure out how you worked around the problem, so that deserves a comment.

So, I guess that it's not currently allowed that one compiled procedure call another compiled procedure?

@Annonymouse Actually I ended up not needing to use the last of those four assumptions because your conditions are equivalent to every equation either being vacuous or having somewhere an "h name". It doesn't matter which side or whether it's isolated.

@Carl Love The code in the Answer above has been vastly simplified. The functionality is identical to what it was before. I just made use of the facts that

remove(s-> ormap(not F(s), ...), ...)

is equivalent to

select(s-> andmap(F(s), ...), ...),

and

indets(e, t) <> {}

is equivalent to

hastype(e,t).

 

@Christopher2222 

I think that you may be making the mistake of thinking that

remove(s-> true in F(s))

and

remove(s-> false in F(s))

(for any function F that returns a set of booleans) are opposites or complements. They aren't because the sets in general contain both true and false. The complement of remove(...) is select(...).

From the examples, I guess that I can make the following assumptions about the relevant hs:

  • that they are always lowercase (h rather than H)
  • that they are always the last character of the main stem of the name (the part of the name before any index or subscript)
  • that subscripting, when used, is always of the form A[B] rather than A__B
  • that the left side of every equation is simply a name, as would be the case if these equations were returned by solve.

If I can make these assumptions, it will simplify the code. If these assumptions are not valid, a solution is still possible.

First 338 339 340 341 342 343 344 Last Page 340 of 709