Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

I was the author of the earlier Answer that you linked. Yes, that algorithm was optimized for and only possible for p=2.

Here's an implementation of Lucas's theorem for any prime p:

`mod/Binomial`:= proc(N::nonnegint, K::nonnegint, p::prime)
description "Computes binomial(N,K) mod p, p::prime, by Lucas's theorem";
option
   author= "Carl Love <carl.j.love@gmail.com> 30-Oct-2018",
   reference= "https://en.wikipedia.org/wiki/Lucas%27s_theorem"
; 
local r:= `if`(K > N, 0, 1), n:= N, k:= min(K, N-K);
   while k > 0 and r > 0 do 
      r:= r*irem(binomial(irem(n,p,'n'), irem(k,p,'k')), p) 
   od;
   r mod p
end proc:

Example of usage:

Binomial(100000, 50000) mod prevprime(50000);
      4

If you know that the number of boundary conditions (BCs) required for the system should be n (because you know how to count the differential order) and the error message says that you need more than n, then the cause is an extra symbolic parameter (symbolic = not numerically specified). This is often because you've spelled a parameter name different ways in your input.

You can list, in order[*1], all the "top-level"[*2] names used in any Maple expression expr (including a container expression such as a list or set of differential equations) by

indets(expr, name); 
lprint(%);

(The command lprint(%) simply makes the output of whatever immediately previous command easier to read "with a fine-tooth comb", and it's not necessary if the first command already makes what you're looking for obvious.)

The reason that dsolve complains the way that it does (rather than just saying "unspecified parameter found") is that you can have dsolve(..., numeric) solve for unspecified parameters of ordinary differential equation (ODE) boundary value problems (BVPs) by giving one extra BC for each such parameter.

Read my recent Post "Numerically solving BVPs that have many parameters". Just concentrate on the first few sections that cover the inputting of the BVP system. The later sections cover elaborate plotting techniques that may be "too much information" for you at this point.

[*1]The order is fixed and chosen by Maple, but it usually bears some resemblence to alphabetical order.

[*2]Names that only occur in indices are not "top-level" and are not listed seperately. Since they're listed with their "parent" names anyway, this is usually not an issue of concern. (Example: For t[final]final is the index and t[final] is the parent. The indets command that I gave would only list the latter.)

The bounds used to plot a function of two variables (by any of the numerous commands that do that) can usually be specified as the limits of integration of the corresponding iterated (double) integral, as in Kitonum's answer. This remains true in alternate coordinate systems: sphericalcylindrical, etc. I find this knowledge to be a great help to "solidifying" my mental visualization of 3-D problems.

It can be done like this:

subsop((indexList=~ ())[], L);

However, this is an inefficient operation for long lists, so if you find yourself needing to do this often, consider replacing lists with tables or Vectors.

It can be done like this:

(n,k):= (6,2);
mul(add~(combinat:-choose([x||(1..n)], k))) = 0

 

The command plot3d is for plotting objects that are locally two-dimensional (what we commonly call surfaces) in three-dimensional space. What you want to plot in 3-D is an object that is locally one-dimensional (commonly called a curve). The command for that is plots:-spacecurve.

If you can figure out how to use it in this case, great, please post the results. If you have trouble figuring it out, I will provide further help.

I can't reproduce your situation; however, this may help, so please try it and let me know how it goes: A restart command should always be separated into its own execution group. The problems that can arise if you don't do this are often weird and difficult to explain and often involve the display of output.

Also: Although it is not an error and certainly not a source of any print problems, you should remove the line iter:= iter+1 from the loop. The for statement automatically increments its index variable for each iteration of the loop. (This is also true for the for statements in every other computer language that I know that has a for statement.) You should always strive to avoid making assignments to a for loop index inside the loop. It itself is not an error, but it is frequently a source of erroneous results.

Consider changing your style to something like this:

eps:= 1e-12; itermax:= 10; Error:= 10;  
for iter from 0 while Error>eps and iter<itermax do
   print("iteration", iter, "Error", Error); 
   Error:= Error*0.1;      
end do;

or this:

eps:= 1e-12; itermax:= 10; Error:= 10;  
for iter from 0 to itermax-1 while Error>eps do
   print("iteration", iter, "Error", Error); 
   Error:= Error*0.1;      
end do;

These are more-normal styles than what you had, and are easier for humans to read.

Records are simplified modules, and they're indexed exactly the same way: r:-ar:-bYou're missing the second character of the indexing operator.

Unfortunately, convert(v, float) doesn't do anything when v is a string. To convert a string that represents a number to a float, you can use evalf(parse(v)). The whole code is

v:= GetProperty(TextAreaRadian,value);
exv:= evalf(parse(v));
SetProperty(TextAreaDegree, 'value', `if`(exv::numeric, sprintf("%6.2f", evalf(exv*180/Pi)), NaN))

This is my first time coding and embedded table, so there's no doubt a better way to do this.

The parse will give an error for strings that don't represent valid Maple expressions. Perhaps that's okay. If that's not okay, the error can be trapped with try.

This is called floating-point contagion. Once even one decimal point is placed into an expression, they tend to infect all expressions derived from that expression, and they tend to spread further around within those derived expressions.

Some tips:

  1. Never use decimal numbers where a simple fraction will do. Most especially, don't do it in exponents.
  2. Put off as long as possible the introduction of any decimal numbers at all into the computations. It's fine (and even a good idea) to enter required decimal data at the beginning of a worksheet and even to give those numbers names. Just don't use them in the early computations.

Explanation: Every time passes through a layer of brackets, it counts as one function evaluation which removes one layer of quotes.

Suppose that I replace the square brackets with functions B1 and B2 (which'll be defined slightly differently from each other) and I use evalapply to define what is done with the (x) outside the functions. Try this code: 

restart:
B1:= (x::uneval)-> 'procname'(x):
B2:= x-> 'procname'(x):
`print/B1`:= `[]`:
`print/B2`:= `[]`:
`evalapply/B1`:= (B::uneval,  x::uneval)-> map(`?()`, B, x):
`evalapply/B2`:= (B,x)-> map(`?()`, B, x):
trace(B1, B2, `evalapply/B1`, `evalapply/B2`):
B1(B1(''z1+z2''))(x); lprint(%);
B2(B2(''z1+z2''))(x); lprint(%);

Does that help your understanding?

Status: It's a feature. I don't mean in the sense that it was explictly designed with this behavior in mind. I mean that this behavior is a natural and logical result of the overall design.

Both of these independence tests are quite easy computationally. The results under method= Pearson (the default) should be identical to those provided by Statistics:-ChiSquareIndependenceTest. I compute it explictly here for readers who just want to see the p-value computation without a lot of elaborate code for input error checking and output formatting. 

ChiSqIndTest:= proc(O::Matrix, {method::identical(Pearson, G):= 'Pearson'})
description "Returns p-value for Pearson's or G chi-squared independence test.";
option
   author= "Carl Love <carl.j.love@gmail.com>, 25-Oct-2018",
   reference= (
      "https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test",
      "https://en.wikipedia.org/wiki/G-test"
   )
;
uses AT= ArrayTools, St= Statistics;
local 
   C:= AT:-AddAlongDimension(O,1), R:= AT:-AddAlongDimension(O,2), #column & row sums
   r:= numelems(R), c:= numelems(C), #count of rows & columns
   #matrix of expected values under null hypothesis (independence):
   T:= Matrix((r,c), (i,j)-> R[i]*C[j], datatype= float) / add(R)
;
   #p-value by either method:
   1 - St:-CDF(
      ChiSquare((r-1)*(c-1)), 
      add(`if`(method='G', 2*O*~ln~(O/~T), (O-T)^~2 /~ T))
   )
end proc:

Example of use:

DrugTrial:= <20, 11, 19; 4, 4, 17>:
ChiSqIndTest(DrugTrial, method= Pearson);
                       0.0402558477482379
ChiSqIndTest(DrugTrial, method= G);
                       0.0358413972184247

 

To get a numeric solution, you'll need numeric values for the parameters, an initial value for x (other than 0), and initial conditions. Then do

ode:= 
   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,
   diff(y(x),x) = Y(x)
:
ICs:= Y(1)=1, y(1)=1:
Sol:= dsolve({ode, ICs}, numeric, parameters= [A,B,C,D4,E,F,G,H]):
Sol(parameters= [1$8]):
plots:-odeplot(Sol, [x,y(x)], x= 1..2);

 

It's much easier than that. Simply do not load the Units package or any subpackage. Top-level Maple also understands units and the Unit(...multiplier. By using Standard, you force the conversion to watts, because "hour" and "day" are not "standard" units.

I suppose that you simply want to treat the function as if it were a variable, without incorporating all the minutiae of the Physics package. Then it can be done in a one-line procedure using freeze and thaw like this:

Fundiff:= (e::algebraic, f::function)-> (F-> thaw(diff(subs(f= F, e), F)))(freeze(f)):

So, if you want the derivative of sin(f(t)) wrt f(t) to be cos(f(t)), then do

Fundiff(sin(f(t)), f(t)).

Applying this to the situation from your other thread, you could do

Fundiff~(SimplifiedMassMatrixDot, theta[2](t))

(The other thread is convoluted, with many questions having already been answered, so I agree with starting a new thread.)

First 158 159 160 161 162 163 164 Last Page 160 of 395