MaplePrimes Questions

Is there a reason, why one can't issue restart command (or any command) after local server losses connection and crashes? 

Each time this happens, I have to close the worksheet and reopen it again for the worksheet to respond again.

But I would have expected the worksheet to see the server it was communicating with has crashed and to restart new one when new command is issued. Very easy to reproduce.  I start a new worksheet  and type in it


#in new cell, type
convert(integrand,parfrac); #also using convert(integrand,parfrac,x); #same result

Now server.exe crashes.

Now I close the above pop-up windows by clicking on the X.

Now I go back to the first cell where the restart command at. Click ENTER on it to evaluate it and nothing happens. Worksheet is frozen.

Actually no command evaluate any more. The whole worksheet do not respond anymore.

Only way is to close the worksheet and open it again. 

My question is: Is this by design?   

Why exactly does the worksheet stop responding unless one closes it and reopen it? 

It seems the front end does not know the server it was connected with has crashed and it sitting there waiting for the server to reply and that is why it hangs. But then how come it generated the pop-up window saying it lost connection?

So it must know. But if it knows the server it was connected to has crashed then why does it not automatically start new one when a new command is issued without one having to close and reopen the document each time?  Is starting a new server can only be made when a worksheet is first opened? But why?

I have it setup that each worksheets uses it own engine. i.e. under options-general I have  "create a  new engine for each document" which is a very useful option. This way when one worksheet hangs or server crashes (which for me happens 10-20 times each and every day), then no other worksheets are affected.


Windows 10.


I have the following procedure that uses the Lie Derivatives of a vector field to build a set of equations.

description "This function returns the system of equations based on the Lie derivative.":
local i,n:=numelems(vars),L:=Array(0..n):
for i from 1 to n do
    L[i]:=inner(f,map((a,b) -> diff(b,a),vars,L[i-1])):
end do:
end proc:

Below it is an example on how to call the procedure.

I used CodeTools:-ThreadSafetyCheck to check all the procedures used within LieDerList and LieDerList itself, but nothing wrong came out. However when I try to run 


where models4 is a list of 1765 elements, maple returns "Error, (in factors) attempting to assign to `LinearAlgebra:-Modular:-Create` which is protected". If I change Threads to Grid, there is no problem at all.  

What am I overlooking? Is there a method to ensure the procedure is thread-safe?

Many thanks.   

PS.  I found one problem - inner, which is related to LinearAlgebra package, is not thread-safe.  

This is not new in Maple 2024, and also happens in Maple 2023. But it causes the famous "kernel connection has been lost" each time. 

I think many of the kernel connection has been lost problems I see might be related. It could be memory problem in this case. I wonder if someone will be able to track what causes it. Is it expected that server.exe crash on such input?

ps. Reported to Maplesoft support.





`Standard Worksheet Interface, Maple 2024.0, Windows 10, March 01 2024 Build ID 1794891`


`The "Physics Updates" version in the MapleCloud is 1708 and is the same as the version installed in this computer, created 2024, March 27, 16:20 hours Pacific Time.`









For reference, another software give this result instantly and with no crash:

My PC is windows 10, with 128 GB RAM.


May be someone could find what causes this new internal error in Maple 2024. 

I did report it already to Maplesoft. It does not happen in Maple 2023. Attached both worksheets.


`Standard Worksheet Interface, Maple 2024.0, Windows 10, March 01 2024 Build ID 1794891`


`The "Physics Updates" version in the MapleCloud is 1708 and is the same as the version installed in this computer, created 2024, March 27, 16:20 hours Pacific Time.`




Error, (in int/gparse/gmon) too many levels of recursion






`Standard Worksheet Interface, Maple 2023.2, Windows 10, November 24 2023 Build ID 1762575`




2*x*(c*exp(n*ln(x))-1)*(c*exp(n*ln(x))+1)*exp(m*(ln(d)+ln(x)-((1/2)*I)*Pi*csgn(I*d*x)*(-csgn(I*d*x)+csgn(I*d))*(-csgn(I*d*x)+csgn(I*x))))/(b*c*n*exp(n*ln(x))*(-b*ln(1-c*exp(n*ln(x)))+b*ln(c*exp(n*ln(x))+1)+2*a))+int(-2*exp(m*(ln(d)+ln(x)-((1/2)*I)*Pi*csgn(I*d*x)*(-csgn(I*d*x)+csgn(I*d))*(-csgn(I*d*x)+csgn(I*x))))*(c^2*m*(exp(n*ln(x)))^2+c^2*n*(exp(n*ln(x)))^2+(exp(n*ln(x)))^2*c^2-m+n-1)/(b*c*n*exp(n*ln(x))*(-b*ln(1-c*exp(n*ln(x)))+b*ln(c*exp(n*ln(x))+1)+2*a)), x)




I setup my package to display the a message when it is loaded. It is quiet convienent but I don't need it all the time. Obviously I can "#" in the code to hide it premanently. I was wondering if there is away to optionally turn it off/on. Something along the lines.

with(RationalTrigonometry,false) or with(RationalTrigonometry)[false]....

Or put something in the .ini flie to set the default behaviour.

 "Default global settings:-

     GeomClr = "Blue",

      Prntmsg = true,

       Prjpsn = 3 can be set to 1,

      Normalgpt = 1 or set to 0, 

    Metric is a 3 x3 symmetric matrix defaults to the Identity 

    matrix "


For a lesson I'm preparing, I want to illustrate some probability concepts using Maple.
In particular, I need to use the fact that the Expectation operator(let say the Mean operator) is a linear operator with respect to random variables. 
However, I don't want to particularize my demonstration by using this or that statistical distribution but simply the notion of random variable.
I have therefore created a specific Distribution named  MinimalAbstractDistribution in which only the mean and variance are defined.

When Statistics:-Mean is applied to the expression (A*p+q) where p and q are names and A is a random variable with distribution MinimalAbstractDistribution, linearity is effectively used.
But not when it is applied to A/p or A-q.

Why that?
Is there a way of defining a statistical distribution so that Mean behaves as expected?

(You will easily understand that no workaround of the form

eval(%, q=-q);

# or

can be accepted in a lesson).

Thanks for your attention




MinimalAbstractDistribution := proc(i)
  Distribution(Mean=mu[i], Variance=sigma[i]^2)
end proc:

A := RandomVariable(MinimalAbstractDistribution(1))



[Mean, Variance](A);

[mu[1], sigma[1]^2]







# But








How do we define a matrix or a vector of the partial differential operator?

Both uses of evalf

evalf(Pi, 20);

work, but only the latter is documented. Is there a reason behind (maybe historical)? Any reason not to use evalf(...,n) any more? I am also reluctant to update old worksheets if there is no need for the indexed version.

Suppose I have, a row vector  R = [R1 , R2  , R] and a column vector C =  [C1 , C2  , C]. I need a multiplication like as follows

RC = [ R1 [C], R2[C], R3 [C]  ], so that RC will be a 3/3 matrix. 

hi   is there any method to tell maple do a paticular auto substitution  ,see picture  below.

i hope  this substitution can be done automatically , instead of using algsubs(signum()^2=1,eq(2)) 

i have tried simplify()  , but did not work.



Recognizing Handwritten Digits with Machine Learning



Using the DeepLearning  package, this application trains a neural network to recognize the numbers in images of handwritten digits. The trained neural network is then applied to a number of test images.


The training and testing images are a very small subset of the MNIST database of handwritten digits; these consist of 28 x 28 pixel images of a handwritten digit, ranging from 0 to 9. A sample image for the digit zero is .


Ultimately, this application generates an vector of weights for each digit; think of weights as a marking grid for a multiple choice exam. When reshaped into a matrix, a weight vector for the digit 0 might look like this.

When attempting to recognize the number in an image



If a pixel with a high intensity lands in the red area, the evidence is high that the handwritten digit is zero


Conversely, if a pixel with a high intensity lands in the blue area, the evidence is low that the handwritten digit is zero


The DeepLearning package is a partial interface to Tensorflow, an open-source machine learning framework. To learn about the machine learning techniques used in this application, please consult these references (the next section, however, features a brief overview)








We first build a computational (or dataflow) graph. Then, we create a Tensorflow session to run the graph.


Tensorflow computations involve tensors; think of tensors as multidimensional arrays.



Each 28 x 28 image is flattened into a list with 784 elements.


Once flattened, the training images are stored in a tensor x, with shape of [none, 784]. The first index is the number of training images ("none" means that we can use an arbitrary number of training images).



Each training image is associated with a label.



Labels are a 10-element list, where each element is either 0 or 1


All elements apart from one are zero


The location of the non-zero element is the "value" of the image


So for an image that displays the digit 5, the label is [ 0,0,0,0,0,1,0,0,0,0]. This is known as a one-hot encoding.


All the labels are stored in a tensor y_ with a shape of [none, 10].



The neural network is trained via multinomial logistic regression (also known as softmax).


Step 1

Calculate the evidence that each image is in a selected class. Do this by performing a weighted sum of the pixel intensity for the flattened image.


e__i = sum(`W__i,j`*x__j, j = 1 .. 784)+b__i





Wi,j and bi are the weight and the bias for digit i and pixel j. Think of W as a matrix with 784 rows (one for each pixel) and 10 columns (one for each digit), and b is a vector with 10 columns (one for each digit)


xj is the intensity of pixel j


Step 2

Normalize the evidence into a vector of probabilities with softmax.


y__i = softmax*e__i and softmax*e__i = e^x__i/(sum(e^x__j, j = 1 .. 784))


Step 3

For each image, calculate the cross-entropy of the vector of predicted probabilities and the actual probabilities (i.e the labels)


H__y_(y) = -(sum(y_[i]*log(y__i), i = 1 .. 10))



y_ is the true distribution of probabilities (i.e. the one-hot encoded label)


y is the predicted distribution of probabilities


The smaller the cross entropy, the better the prediction.


Step 4

The mean cross-entropy across all training images is then minimized to find the optimum values of W and b



For each test image, we will generate 10 ordered probabilities that sum to 1. The location of the highest probability is the predicted value of the digit.



This application consists of



this worksheet


and a very small subset of images from the MNIST handwritten digit database


in a single zip file. The images are stored in folders; the folders should be extracted to the location as this worksheet.

Load Packages and Define Parameters



TRAIN_STEPS   := 40:


Number of training images to load for each digit (maximum of 100)

N := 22:

Number of labels (there are 10 digits, so this is always 10)

L := 10:


Number of test images

T := 50:

Import Training Images and Generate Labels


Import the training images, where images[n] is a list containing the images for digit n.

path := "C:/Users/Wilfried/Documents/Maple/Examples/ML/":
for j from 0 to L - 1 do
    images[j] := [seq(Import(cat(path, j, "/", j, " (", i, ").PNG")), i = 1 .. N)];
end do:

Generate the labels for digit j, where label[n] is the label for image[n].

for j from 0 to L - 1 do
   labels[j] := ListTools:-Rotate~([[1,0,0,0,0,0,0,0,0,0]$N],-j)[]:
end do:


Display training images

Embed([seq(images[i-1], i = 1 .. L)]);



Flatten and collect images

x_train := convert~([seq(images[i - 1][], i = 1 .. L)], list):


Collect labels

y_train := [seq(labels[i - 1], i = 1 .. L)]:


Define placeholders x  and y to feed the training images and labels into

x  := Placeholder(float[4], [none, 784]):
y_ := Placeholder(float[4], [none, L]):

Define weights and bias

W := Variable(Array(1 .. 784, 1 .. L), datatype = float[4]):
b := Variable(Array(1 .. L), datatype = float[4]):


Define the classifier using multinomial logistic regression

y := SoftMax(x.W + b):


Define the cross-entropy (i.e. the cost function)

cross_entropy := ReduceMean(-ReduceSum(y_ * log(y), reduction_indicies = [1])):


Get a Tensorflow session

sess := GetDefaultSession():


Initialize the variables

init := VariablesInitializer():


Define the optimizer to minimize the cross entropy

optimizer := Optimizer(GradientDescent(LEARNING_RATE)):
training  := optimizer:-Minimize(cross_entropy):


Repeat the optimizer many times

for i from 1 to TRAIN_STEPS do

   sess:-Run(training, {x in x_train, y_ in y_train}):

   if i mod 200 = 0 then
      print(cat("loss = ", sess:-Run(cross_entropy, {x in x_train, y_ in y_train})));    
   end if:

end do:

Import Test Images and Predict Numbers


Randomize the order of the test images.

i_rand := combinat:-randperm([seq(i, i = 1 .. 100)]);

[13, 71, 67, 52, 81, 37, 46, 6, 39, 77, 36, 21, 49, 95, 62, 26, 44, 65, 90, 72, 70, 5, 4, 54, 31, 23, 63, 18, 22, 38, 27, 53, 50, 17, 47, 51, 78, 79, 92, 20, 28, 34, 60, 80, 58, 87, 86, 93, 84, 12, 59, 98, 97, 56, 75, 10, 29, 61, 7, 66, 100, 42, 91, 43, 89, 76, 11, 74, 8, 96, 64, 94, 68, 48, 33, 24, 40, 30, 57, 73, 99, 15, 19, 1, 3, 41, 85, 83, 35, 14, 45, 2, 88, 9, 16, 32, 69, 25, 55, 82]


Load and flatten test images.

path:= "C:/Users/Wilfried/Documents/Maple/Examples/ML/test_images":
x_test_images := [seq(Import(cat(path,"/","test (", i, ").png")), i in i_rand[1 .. T])]:
x_train:= convert~(x_test_images, list):


For each test image, generate 10 probabilities that the digit is a number from 1 to 10

pred := sess:-Run(y, {x in x_train})



For each test image, find the predicted digit associated with the greatest probability

predList := seq( max[index]( pred[i, ..] ) - 1, i = 1 .. T )

9, 1, 0, 5, 3, 4, 5, 2, 8, 3, 8, 2, 5, 2, 4, 6, 8, 4, 1, 1, 1, 6, 7, 5, 7, 7, 4, 7, 7, 8, 7, 5, 5, 7, 5, 5, 3, 3, 0, 6, 7, 7, 4, 3, 4, 0, 3, 2, 3, 7


L := []; for i to 50 do L := [op(L), max(pred[i])] end do
for k from 1 to 10 do:
 for i from 1 to 50 do:
  if predList[i]=k-1 then L1:=[op(L1),L[i]] end if:
 end do:
end do:



Vector[column](%id = 36893490552619428548), Vector[column](%id = 36893490552619428668)


Consider the first test image


The ten probabilities associated with this image are

pred[1, ..]

Vector[row](10, {(1) = 0.1176837849925505e-4, (2) = .3597199022769928, (3) = 0.8788742707110941e-3, (4) = 0.14628235250711441e-1, (5) = .16885940730571747, (6) = 0.10462711565196514e-1, (7) = 0.16997022554278374e-1, (8) = 0.5874206870794296e-1, (9) = 0.20698020234704018e-2, (10) = .3676302134990692})



Confirm that the probabilities add up to 1

add(i, i in pred[1, ..])




The maximum probability occurs at this index

maxProbInd := max[index](pred[1, ..])



Hence the predicted number is

maxProbInd - 1



Embed(x_test_images[1 .. 25])

Embed(x_test_images[26 .. 50])

We now display all the predictions

T1 := Table(Row(seq(predList[k],k = 1.. 25)),Row( seq(predList[k],k = 26 .. 50 ))

Visualize Weights

I have problmes running the file with Maple 2024. It runs fine with Maple 2020.2 (execpt the very last part, which is not essential). The problem occurs at the SoftMax command, even if I use Softmax. It seems to be a Python conersion problem in Maple 2024. Please let me know what the remidy is. You need to modify the data path because it is set to my computer.



Can I draw such figures using Maple?

If possible please guide how I can do it.

How to evaluate the right eigenvector of a given matrix in maple?

This is just cosmotics, but it looks ugly for me. For some reason Maple converts exp(2*a) to (exp(a))^2 under certain operations such as expand



This happens in worksheet under typesetting level extends or standard.

Any specific reason why Maple likes to rewrite exp(2*a) as (exp(a))^2  and is there a way to tell it not to do that?

ps. it is little more than cosmotic actually, it affects the Latex generated


{\mathrm e}^{2 a}

\left({\mathrm e}^{a}\right)^{2}


Maple 2024 on windows 10

I made a dsolve for a system of differential equations, and I get a set of solutions. How to transform them into  functions that can be displayed?

3 4 5 6 7 8 9 Last Page 5 of 2313