Carl Love

Carl Love

28085 Reputation

25 Badges

13 years, 99 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

Here's a simple way to construct your matrices, multiply them, and force through the explicit multiplication of parenthesized factors:

n:= 6: # order of matrix
# exp(I*z) = cos(z) + I*sin(z)
A:= Matrix(n$2, (i,j)-> exp((1-i)*(j-1)*2*Pi*I/n)); 
expand~(A.A^*);

Notes (some of which were covered by others above):

  • # indicates the beginning of a comment, which continues to the end of the line.
  • sqrt(-1) in Maple is I, not i, unless you change that default with interface(imaginaryunit= ...).
  • For a matrix A, the conjugate transpose is A^*. Since your matrix is symmetric, it's effectively just the conjugate.
  • The expand command forces through the multiplication of parenthesized factors.
  • For any operation OP that you want to apply to the elements of a matrix M, you can make the operation apply to all the elements by OP~(M).
  • One of the most important identities in all of mathematics is exp(I*z) = cos(z) + I*sin(z). This doesn't change your matrix at all; it merely simplifies how you input it.

You can upload a worksheet to this website by using the green up-arrow on the tool bar of the editor. Nearly all of us who answer questions here prefer this to (or in addition to) screenshots because that way we don't need to retype your input into Maple.

In the general case that the Vs are arbitrary functions (expressions in a single-variable x, or constants), you can use piecewise as in

plot(piecewise(seq([x < L[k], V[k]][], k= 1..numelems(L))), x= 0..L[-1]);

If the Vs are just line segments or just constants (as in your example), then a more-efficient solution is possible, if you care, although the piecewise solution is not notably inefficient.

Note that I'm simply using the plot command; plots[multiple] isn't needed, or desired, for this.

Of course, my command above assumes that

  1. L and V are indexable structures with the same number of elements and indices starting at 1 (such as Vectors or lists);
  2. the Ls are distinct positive constants sorted in increasing order;
  3. you wish the domain to start at x=0.

If any of those are not true, my command can be adjusted to accomodate that.

 

In addition to what Acer said, which is totally correct, I thought that I might give a more-thorough answer to the "How?" question that you directly posed at the end of your Question. Take a look at this simple recursive code, which is what's ultimately executed by your convert command:

showstat(`convert/list`::convert_rtable_to_nested_list);

You'll see that there's no magic efficiencies, no special provisions for sparsity, no special provisions for order, and no special handling of zeros. It's just a recursive loop whose base case is simple indexing, as in A[...].

Integration with respect to functions as opposed to with respect to simple variables is not something handled by int (well, at least not in its common usage). If you assign expressions to x and y, then you can't integrate with respect to x or y.

Most packages are implemented as modules. Anything that can be accessed in the form A:-B comes from a module named A. Modules have two types of local variables: regular (private) locals and (public) exports. Ordinarily, only exports can be accessed with the :- operator. But, if you issue

kernelopts(opaquemodules= false);

then you'll be able to access private locals also.

Another way to see module A's local procedure B is

showstat(A::B);

This was only implemented a few versions ago, so I don't know if you have it. Regardless, the opaquemodules trick will definitely work.

Note that it's ithratB, not ithratb. Also, showstat(ithratB) will never work. It either has to be showstat(numtheory:-ithratB) or showstat(numtheory::ithratB) (or <*cringe*> showstat(numtheory[ithratB]) (*footnote)). Dropping the module-name prefix is only allowed for exports and then only in the context of a with or use command or a uses clause.

(*footnote): The use of the syntax A[B] as a general substitute for A:-B is something that I strongly disdain. The A:-B is precise: It can only mean that B is literally and explicitly, without any further evaluation, a local or export of module A. The syntax A[B] can mean a great many things and even if it's known that A is a module, it's B's evaluated value that gets looked up. The A[B] is only acceptable to me when B needs to be evaluated first. Unfortunately the Maple documentation is loaded with the inappropriate use of this syntax.

Something that I occasionally find useful along these lines is kernelopts(memusage). It returns a listlist (which, oddly, displays as a Matrix---I guess kernelopts has special privileges of display?) of three columns: The first is labels for 63 categories of possible objects stored in memory, the second is the number of objects stored in each of those categories, and the third is the number of bytes stored in each of those categories. So, you can call it before and after calling a suspect procedure and take the difference of the two readings.

I believe---but I'm not sure---that this command gives you a true partition of all stored data, i.e., every byte of data that you currently have stored appears in exactly one of the 63 categories.

Two things needed: 

  1. Replace U and V with U(x,t) and V(x,t) throughout pde1 and pde2.
  2. The first argument to pdsolve should be {pde1, pde2}.

These are necessary to solve your problem but not necessarily sufficient. I can't get on my computer right now to check. (There's a "no laptops" sticker on the café table I'm at.)

How do you expect n! to be defined for negative integers other than by extension to GAMMA?

I think that Maple is primarily designed for high and wide computational power rather than to be educational or didactic. That requires that most functions be defined over the largest part of C possible. But, you can restrict the domains using assuming. In this case, you should append assuming n::posint to the original product. The resulting formula will contain GAMMA functions, but it'll evaluate correctly for your range of regardless of whether you convert to factorial.

It seems that your attempt to upload the worksheet didn't work. Could you try again please?

Are you trying x[1] or x__1 for the variable? If it's either of those, it's surely the first. And did you tell it the name stem that you wanted to use? In that case, tell it to use :-x (colon hyphen x). Or did you let it choose the name stem? Then it must be _x[1]. Unassigned global names that library procedures generate for output always begin with underscore.

x[1] is a name with an index subscript; x__1 is a name with an inert subscript. They display the same (as subscripts) in pretty-printed output.

If you have output that you don't understand how to type, often applying the command lprint to that output improves understanding:

lprint(%);

Use expand rather than simplify. That's just being specific about the type of simplification that you want.

Your sum stops at m. I think that should be n.

The best way to fix the mantissa is to set the Digits environment variable. Maple internal floating-point arithmetic is base 10 with an integer mantissa with maximum of Digits digits. If I wanted to show the same anomaly of floating-point computation, I'd use this loop:

HarmonicSum:= proc(
   digits::posint:= Digits,
   rounding::identical(0, nearest, -infinity, infinity, simple):= Rounding
)
local S0:= -1., S1:= -1., S2:= 0., k;
   UseHardwareFloats:= false; #Optional
   Digits:= digits;
   Rounding:= rounding;
   for k to infinity while S2 > S1 do
      (S0,S1,S2):= (S1, S2, S2 + 1./k)
   od;
   ['n' = k-2, 'S'[k-3]=S0 , 1/(k-2) = 1./(k-2), 'S'[k-2]=S1, 1/(k-1)=1./(k-1)]
end proc: 

I think that that makes it explicit what's going on, right? And it gives you control over the order that things are added, which is important for this example.

HarmonicSum(7);

The output is the number of terms, the penultimate partial sum, the final term, the final sum, and the first term not used because it didn't change the sum. Because of the base-10 arithmetic, it's really easy to see what's going on.

If x is a Maple float, then [op(x)] will return the mantissa and exponent as an ordered pair.

Update: I added an option to allow the user to set the rounding mode.

When you're composing a post on MaplePrimes, the toolbar in the editor has a green up-arrow. It's the third item from the right in the top row of the toolbar. Let me know if you have any further trouble with uploading.

This is all that you need to do: Somewhere after the end proc of the definition of procedure si but before your usage of si, do

si:= subs('s'= s, eval(si)):

That's all!

When subs is used on a procedure in this way, it has the profound, almost spooky or magical, effect of changing global variables into parameters or locals, whichever is appropriate for the situation. This usage of subs is not documented on its help page (?subs), and I don't know if it's officially documented anywhere, but it is extensively used in Maple library code, so I don't think that it's ever going away. This usage of subs provides an extremely powerful technique for dynamic code generation.

In actual usage, I'd keep si as a "procedure-schema" and use the subs to create a procedure of a different name, as in

si1:= subs('s'= s, eval(si));

That way you can re-use si if you change to a new s. Also, it's not necessary that the s inside the procedure and the s outside have the same name. Indeed, it'd preferable that they were different. I often see experts using a name like _DUMMY_s for the one inside the procedure.

This usage of subs is similar to using unapply. The limitation with unapply is that it only works on a single expression, not a whole procedure. From Acer's code and Joe's comment about it, you can see that it wasn't super-super-easy to mangle the if statement from your procedure into something that unapply would accept. (Well, I'm sure it was easy for them, but they're the two greatest Maple programmers in the world IMO.) If it had been a do loop, it may have been impossible. If you read the code of unapply (by doing showstat(unapply)), you'll see that it's using the subs-into-procedure trick anyway.

 

The Maple command is timelimit. See ?timelimit

Example:

MyComputation:= ()-> 
   isprime((3^nextprime(rand(2^20..2^30)())-1)/2)
:
try
   r:= timelimit(10, MyComputation())
catch "time expired":
   r:= "Time expired."; #And proceed without error.
catch: #Some non-time-related error occurred:
   error
   #The actual error message will be printed at execution time.
end try;

 

Applying the methods from either of the Answers above could produce a negative result if the data were different or if simply the order that they were presented was different. This is not an error, but you do need to take the absolute value. So, to be general, you should use

abs(TripleScalarProduct(P1-P5,P6-P5, P8-P5))

or

abs(V1 &x V2 . V3)

The triple scalar product can also be computed as a 3x3 determinant, so my symbol-laden entry is

P||(1,5,6,8):= 
   <-17/12,11/36,-65/36>, 
   <-11/12,-7/36,-47/36>,
   <-2/3,-1/9,-8/9>,
   <-5/4,-1/12,-17/12>
:
abs(LinearAlgebra:-Determinant(`<|>`(map(`-`, [P1,P6,P8], P5)[])));

 

First 178 179 180 181 182 183 184 Last Page 180 of 395