Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Getting Maple to do those computations is trivial, and there are many ways to do it. I show one way below. But there'll be great variation in the efficiency of those ways depending on how the input is supplied, and how your raw computation sequence is converted to a Maple procedure.

Getting Maple to write out a CSV file is also trivial, and it's done with the command

ExportMatrix(file namematrix name, 'target'= 'csv') 

(see help page ?ImportMatrix).

But I'm confused as to why you "have this file", which contains both one instance of the input data and the computation sequence.

  1. Would it be necessary for Maple to read and interpret the computation sequence from this file?
  2. Or could you just manually place the computation sequence in a Maple worksheet?
  3. Is it necessary that the input data come from such a file?
  4. Or could it come from a CSV file?

Suppose that your answers to 2 & 4 are "yes". Then you could do this:

restart:
MyFunc:= evalf@unapply(
   eval[recurse](
      <T | U>, #Output a 2-element row vector.
      #One of many ways to specify computation sequence:
      [T= E^2+G-sin(F), U= F+E-B, E= A*B, F= cos(A)+exp(B), G= 265/C]
   ), 
   [A,B,C] #Input parameters
):
Process:= proc(In::Matrix)
local n:= upperbound(In)[1], Out:= Matrix(n,2), k;
   for k to n do Out[k,..]:= MyFunc(seq(In[k])) od; #Process input row by row.
   Out
end proc
:
Data:= ImportMatrix( some file name , 'source'= 'csv');
ExportMatrix( some other file name , <Data | Process(Data)>, 'target'= 'csv');

If the answers to 1 or 3 are "yes", it's still possible, but I'll need details.

Is there anything wrong with 

has(expr, sqrt(x*y))

After 

infinies:= Infinitesimals(PDE)

do something like

infinies:= eval(infinies, [_F4= 0, _F3= 0, _F2= 1, _F1= (R-> a*R)])

Then, as you said, the Invariants will be much simpler; indeed, they'll fit on one line and contain no unevaluated integrals.

Note 1: Anywhere in Maple, an explicit numeric constant (such as or 1 above) can be used as if it were a constant function.

Note 2: The underscore characters are essential parts of the names _F1, etc., just as if it were an alphabetic letter.

Warning: The _Fs will not necessarily be numbered the same every time that you run this. I advise that you visually check it each time.

There are three problems that I see immediately, before even running your code:

  1. In your for loop, the four expressions on the left sides of the assignment operators each have an extra space after the function name. In others words, you need to change S (k+1) to S(k+1), etc.
  2. On the right sides of the assignment operators, you need to change all square brackets [ ] to parentheses ( ). Square brackets are never used for algebraic groupings in Maple (nor in most other computer languages).
  3. needs to have a value.

The difference is simply that CentralMoment is treating the data as a whole population and Variance is treating it as a sample. To me, it seems useful to be able to make that distinction, so I like that the two commands return different results.

You wrote:

  • By definition the variance of a random variable X is its 2nd order central moment. This definition should also apply when these statistics are calculated on a sample of X.

But how is the command supposed to know whether the data that you've given it is a sample or the whole population?

Here's a procedure with an optional second argument to specify the depth of de-nesting. If the second argument is omitted, it does exactly what Acer's code does.

Flatten:= proc(S::set, depth::nonnegint:= infinity)
local s, r:= S;
   if depth=infinity then return {subsindets(S, set, op)} fi;
   to depth do r:= {seq(`if`(s::set, s[], s), s= r)} od;
   r
end proc
:

Examples:

A1 := {1,2,{5,6},3,{13,{{21,27},16}},{8,9},{{11,12},4}};
Flatten(A1); Flatten(A1,1); Flatten(A1,2);

 

Do 

remove~(evalb, S)

The image sqrt(R) is a connected set in the complex plane[*1]: It's the union of the nonnegative real and the nonnegative imaginary semi-axes. Just because it's not real-valued for all real arguments doesn't automatically make it discontinuous. 

[*1] By the way, I'm not claiming that a connected image necessarily implies continuity; it doesn't. But by considering this image, it's easy to see that sqrt is an embedding of into and hence continuous.

I don't see any practical difference between what you ask for and 

A:= Array((0..M)$3, ()-> a[args])

Rather, the only difference that I see is that you call it a "block matrix", whereas I call it a "three-dimensional array".

It seems like it would take a great many steps to convert an ODE from Maple standard form (i.e., the form(s) accepted by dsolve) to the specialized form required by Edgardo's suggestion, DifferentialAlgebra:-Tools:-LeadingDerivative. I look forward to seeing an example of it. On the other hand, it looks like it'll work for PDEs also.

Here's a procedure to do it just for ODEs:

ODEdegree:= (e::{algebraic, `=`(algebraic)})-> 
   try
      (e-> degree(
         e, 
         indets(
            e, 
            typefunc(
               (O-> `if`(O>1, typefunc('`@@`'(identical(D),O)), specfunc(D)))
                  (PDEtools:-difforder(e))
            )
         )
      ))
         (evala(Norm(convert(`if`(e::`=`, (lhs-rhs)(e), e), D))))
   catch:
      FAIL
   end try
;

It works for both equations and expressions, and for D-form and diff-form (or even a mixture). Here it is applied to your example:

ode:= (1+diff(y(x),x)^2)^(3/2) = diff(y(x),x$2);
ODEdegree(ode);

      2

It looks like evalc won't automatically assume that D(x11)(1) and D(y11)(1) are real. It will assume that if diff form is used instead of D. So, this gets you your desired result:

convert(evalc(Re(convert(F1,diff))), D)

If you plan on doing a lot of this, I could add code to evalc (specifically, a procedure named `evalc/D`) that would do the above automatically.

There seems to be a critical-severity bug in fsolve in Maple 2019.1 such that every attempt that I've made at solving this with fsolve (using various formulations of the function whose root is wanted) has led to a kernel crash. This is particularly disturbing in this case because the function is differentiable, strictly increasing, and has a unique real root.

Another root-finding command--an alternative to fsolve--is RootFinding:-NextZero. Here it is applied to your problem. I've done this in a way that can be applied to most arclength problems:

restart:

#These first 2 steps are simply to reconstruct your original function
#from its derivative:
C:= 5.557990765:
f:= int(<-C,-7.3,-5.6,7.3>.<[sin, cos, sinh, cosh](C*x)>, x);

x0:= 0: #Start of arclength measurement.
L:= 0.5: #Desired arclength.

F:= subs(  #Construct numeric procedure whose root we want.
   [_ArcLInt= VectorCalculus:-ArcLength(<x, f>, x0..X, 'inert'), _ArcL= L],
   proc(X) local x; evalf(_ArcLInt) - _ArcL end proc
):    
x1:= RootFinding:-NextZero(F, x0);
                       x1 := 0.1550088023
plot(f, x= x0..x1);

An easy way to get a close upper bound on the desired x-value is to use the straight-line distance between two points on the curve:

x_ub:= fsolve((x0-x)^2 + (eval(f, x= x0) - f)^2 = L^2);
                     
x_ub := 0.1570169605

This upper bound might be useful if you're using a root finder whose performance is improved by specifying bounds.

An example of an fsolve that leads to a kernel crash is

fsolve([F], [x0 .. L])

using procedure and x0 and L from above.

To permute the rows of Matrix so that they're sorted (ascending) by column C, do

M[sort(M[..,C], output= permutation), ..]

To change that to descending, make `>` the second argument to sort.

To count the entries of column that are larger than V, do

add(`if`(x > V, 1, 0), x= M[..,C])

You can read the through the code using showstat(fsolve)showstat(fsolve::sysnewton)showstat(fsolve::linsolve), etc.

One eye-opener for me is that linsolve uses a directly coded Gaussian elimination rather than using the extensively optimized LinearAlgebra:-LinearSolve. This is done for every iteration of Newton's method.

It only evaluates when you press RETURN. If instead you end lines with SHIFT-RETURN, you'll get a new line without any evaluation happening. You can enter any  number of lines this way. Each group of such lines is called an execution group.

First 137 138 139 140 141 142 143 Last Page 139 of 395