Scot Gould

Scot Gould

732 Reputation

14 Badges

11 years, 150 days
Claremont McKenna, Pitzer, Scripps College
Professor of Physics
Upland, California, United States
Dr. Scot Gould is a professor of physics in the W.M. Keck Science Department of Claremont McKenna, Pitzer, and Scripps Colleges - members of The Claremont Colleges in California. He was involved in the early development of the atomic force microscope. His research has included numerous studies and experiments using scanning probe microscopes, particularly those involving natural fibers such as spider silk. More recently, he was involved in developing and sustaining AISS. This full-year multi-unit, non-traditional, interdisciplinary undergraduate science education course integrated topics from biology, chemistry, physics, mathematics, and computer science. His current interest is integrating computational topics into the physics curriculum. He teaches the use of Maple's computer algebraic and numerical systems to assist students in modeling and visualizing physical and biological systems. His Dirac-notation-based quantum mechanics course is taught solely through Maple.

MaplePrimes Activity


These are replies submitted by Scot Gould

@Carl Love If I understand what you wrote, sort uses the value from first entry of a row label's list that it obtains from the key function to order the row labels, but if the two values from the two lists are equal, it uses the values from the second entry to compare. Yes?

This is exactly what I want it to do, and it appears to be readable. However, in looking over examples in Help, I saw only one example of using 'key' so I'm not exactly sure how one would be able to know sort will work with a list of entries in the keys. 

Unless you object, I will suggest Maple add this example to the list of examples in both the "sort" help page and the "DataFrame/sort" page.

@Joe Riel Much appreciation for taking the time to think this out. I had been flailing around until now. This line reaffirms your finding about "setorder": 

{ mykeys } # --> { 9 "aa" + 2, 9 "aa" + 8, ... }

I'm not sure it is worth much time in reading more about the ordering of sets given that "This order could be changed in future versions of Maple, and/or objects could be represented in a different way in future versions. You should not assume that sets will be maintained in any particular order."

@Joe Riel Well, that was a LOL moment. In my excitement  to test the 'key' option, I miss interpreted the column "Batteries", which are integers, as the being the column that I want to use as a the primary sorting column: "B-type", which are strings. Hence when using "B-type", the values of the keys are mixed type of strings and numbers. Hence my confusion - why does it work. Hence your confusion of me asking about strings when your key was clearly multiplying numbers. (I'm familiar with the calculation, hence there was no confusion there. But I appreciate the follow up.)

Minimal version of Flashlight DataFrame:

restart; Flashlight := DataFrame(`<|>`(`<,>`("aa", "ba", "aa", "ba", "aaa"), `<,>`(8, 4, 2, 2, 2), `<,>`(1, 2, 3, 2, 1)), columns = ["b-type", "cost", "batteries"])

_m3174014334240

(1)

Sort first by "b-type", then by "cost" - lowest to highest

Prows := [seq(1 .. upperbound(Flashlight, 1))]; CostMax := max(Flashlight["cost"])


Intentionally pulling out the key function so that we can display the values:

"mykey(n) := Flashlight[n, "b-type"]*(CostMax+1)+Flashlight[n, "cost"];"

proc (n) options operator, arrow, function_assign; Flashlight[n, "b-type"]*(CostMax+1)+Flashlight[n, "cost"] end proc

(2)

 

Sort and reorder...

P := sort(Prows, 'key' = mykey)

Flashlight[P, () .. ()]

P := [3, 1, 5, 4, 2]

 

_m3174052322400

(3)

It appears to work.

 

What are values from the  key function?

mykeys := seq(mykey(n), `in`(n, Prows))

9*"aa"+8, 9*"ba"+4, 9*"aa"+2, 9*"ba"+2, 9*"aaa"+2

(4)

Now compare 2 keys.

is(mykeys[1] < mykeys[2])

FAIL

(5)

``

 

Which leaves with my original question - how come this key appears to work?

 


 

Download Flashlight_sort.mw

@Joe Riel I see the calculation output of the 'key' as a number * string + number, where the string could be "aaa".   I don't see how a string = a cell. Maybe you can point me to a reference in help. 

 And you are correct, I tried to switch the primary and secondary columns, and it failed.

Your next version is closer to what I tried to write. It may not be as "efficient" but it has to be far more efficient than a solution that uses recusion. 

@Joe Riel Thank you. Yes, I was trying to use a sorting procedure, but I couldn't get the indexing to work.  'key' is the key.  But more than that, I would never have guessed the multiplication of a string by a number could be used to compare elements. 

@DoingMath2018 I agree that I would like to see more examples in the help pages of Maple.  This is something that MATLAB does better. 

May I suggest a couple ideas for helping you find answers to your questions in the future. 

1) Search MaplePrimes. I entered "procedure multiple plots" and found this post from 2011: https://www.mapleprimes.com/questions/126163-Writing-A-Procedure-To-Output-Multiple-Plots .  This appears to ask essentially the same question that you have asked.  And @acer provided the answer then.

2) Open the Maple Portal worksheet (Toolbar: Help -> Maple Portal) and then select the series, "How do I.."  I refered to one of the tutorials in the list today.

@achreftabet , late last night I read the error in your version of my worksheet too quickly and missed what it says. In addition,  as @acer correctly points out, you are using Maple 18, which was released in 2014. (Maplesoft changed the numbering system to yearly, hence Maple 2018 was released in 2018.) As a consequence of your version, it appears the "rand" function call I used in the post to Kitonum, was one which your version could not execute. 

I don't have access to a copy of Maple 18, so I'm going to guess what needs to be done to correct the error in the line. In addition, I'm going to guess that you have limited experience with Maple, so I'll be more descriptive in my response.

The line in question is: 

a := Array(0..N, 0..N, (i, j) -> rand(-1.0..1.0)) ;

where N is defined as 10.  What this means is that I want to assign (define) to the name (or variable), an array of 2 dimensions for which the indices range from to 0 to 10. In defining this array,  each element, which is labeled by the indicies (i, j), there will be a random floating point number which falls somewhere between -1.0 and 1.0.  The goal of this line is to place some numerical values into each element of the array.

What can you do to make it work on your end?  It appears that if you change the line to say it selects a random integer in a range of -integer to +integer, then it might work. For example:

a := Array( 0..N, 0..N, (i, j) -> rand(-1000..1000)/1000.0 )

But you could also have fun and play around with array by putting numbers into your array using a formula. For example, what if we want the elements to count. 

a := Array(0..N, 0..N, (i, j) -> i*N + j )

If you plan to use Maple more than this one exercise, I suggest investing time to learn about the basics. I believe there is a good Users manual that comes with Maple 18. You can find it under Help. And there are lots of videos at Maplesoft. A good "give me the basics" book I can recommend is "Getting Started with Maple" by Meade, et.al. 

 

 

@achreftabet  Just hit the !!! button at the Toolbar at the top of the program,  and everthing will be executed. By adding a "restart" at the beginning, all values are reset to 'unassigned'.

I'm guessing you are new to Maple. Hence I encourage you to look at the Maple Portal. It includes lots of "How do I" examples. 

 

@Kitonum I'm curious why you chose 'sum' in your solution. Given what one assumes is a discrete value for n, my first inclination was not to use 'sum', but rather 'add' . In comparing the two, I examined some typical examples I have encountered. For me, the most important question was how does one use S if the array elements are assigned values. Here sum failed for me. I would appreciate your thoughts.

 

restart; randomize():

Define S and test it:

S := n -> sum( sum( a[i, j]*x^i*y^j, j = 0..n-i), i=0..n);

proc (n) options operator, arrow; sum(sum(a[i, j]*x^i*y^j, j = 0 .. n-i), i = 0 .. n) end proc

(1)

S(2);

x^2*a[2, 0]+x*y*a[1, 1]+y^2*a[0, 2]+x*a[1, 0]+y*a[0, 1]+a[0, 0]

(2)

Define A and test it:

A := n -> add( add( a[i,j]*x^i*y^j, j=0..n-i), i=0..n);

proc (n) local i, j; options operator, arrow; add(add(a[i, j]*x^i*y^j, j = 0 .. n-i), i = 0 .. n) end proc

(3)

A(2);

x^2*a[2, 0]+x*y*a[1, 1]+y^2*a[0, 2]+x*a[1, 0]+y*a[0, 1]+a[0, 0]

(4)


Assign values random values to the elements  in the array `a`.

N := 10:
a := Array(0..N, 0..N, (i,j)->rand(-1.0..1.0)()):

 

Problem: testing S with n = 2.

S(2);

Error, (in S) bad index into Array

 

Hmm, no longer symbolic.

 

As opposed to A:

A(2);

-.236744530+.396899170*y+.552824653*y^2+.441392168*x-0.95195890e-1*x*y-.153404931*x^2

(5)

S can't be plotted even if the function is delayed:

plot3d( 'S(10)', x=0..1, y=0..1)

Warning, unable to evaluate the function to numeric values in the region; see the plotting command's help page to ensure the calling sequence is correct

 

 

But this does work.

newS := (x,y,n) -> sum( sum( a[i, j]*x^i*y^j, j = 0..n-i), i=0..n);

proc (x, y, n) options operator, arrow; sum(sum(a[i, j]*x^i*y^j, j = 0 .. n-i), i = 0 .. n) end proc

(6)

plot3d('newS(x,y,10)', x=0..1, y=0..1);

 

A behaves as a I would expect.

plot3d(A(10), x=0..1, y=0..1)

 

 


 

Download Sum_vs_Add.mw

 

@acer 

1) The explaination in your response helps me understand what the ':-' in  ':-size' is. I completely misunderstood that part of your code. 

2) Between reading your discussion of "plot persistence" and simplifying your code, I was able to understand why the wrapper worked. Thanks again.  (This post also lead me down a rabbit hole of odd plotting outcomes that I can now attribute to "plot persistence" . )   PlotPersistence_Effect.mw

PS. Your suggestion to me, and to others, to concentrait on evaluation of names process in Maple was probably the most paradigm-shifting moment of the year for me.  I believe much of my misunderstandings come from decades of coding using numerically based environments and thus "assuming" I know what I'm doing.   

@acer I like your idea. It matches MATLAB's version of the command: linspace.

(I noticed this 2 miss when a looked at the failing of seq in the plot of my solution to the original question.)

That said, the step size in seq requires a type numeric, (yes?). So what is the type for Pi, which appears to be not numeric?  I ask because if I were to write procedure that accepts numeric and Pi, how would I write the acceptable types?   x::{numeric, and what ever Pi is.}

 

@acer I like seq, and I see you used it. But I noticed there was an unexpected behavoir when I used it. I can sort of understand line 2, but I don't understand line 3:


 

restart

a := 2*3.15; N := 4; dx := 2*a/(N-1); seq(-a .. a, dx)

6.30

 

4

 

4.200000000

 

-6.30, -2.100000000, 2.100000000, 6.30000000

(1)

a := evalf(2*Pi); seq(-a .. a, dx)

6.273185308

 

-6.273185308, -2.084395102, 2.104395104

(2)

a := evalf(2*Pi); dx := 2*a/(N-1); seq(-a .. a, dx)

6.283185308

 

4.188790206

 

-6.283185308, -2.094395102

(3)

``

``


 

Download seqerror.mw

@acer This workaround certainly useful, but hallenging to understand.  My current expectation is that "size" is "size" is "size" regardless where it is displayed, but if I read your code correctly, the "size" used in a procedure can be different than the size in the global section, hence the use of the terminology ":-size".  And yet, when the image is actually displayed in the global section (is this the correct terminology), it resorts to the :-size, not the size that it was used to generate the image. 

Agreed, I have seen the problem in 2020.1 as well, but I noticed it in the Print-Preview.  (Windows 10).

Yes, forcing a page-break does appear to eliminate the lack of content given that the content is forced onto the next page.  

@Carl Love Completely understandable. I was hoping it would work in all cases. But since my problem has been solved, I don't have an environment to test your idea. I hope it works for krobe8. 

 

New question which stems from seeing your idea: is it possible to be notified about an answer/posting within a question without having to contribute to the question?   

First 12 13 14 15 16 17 18 Last Page 14 of 23