Download MuPAD User's Guide

Transcript
5
Programming Fundamentals
end_if;
end_while;
end_if;
return(FALSE)
end_proc:
Use the op function to access all elements of the matrix M. This function returns a
sequence of elements. Use brackets to convert this sequence to a list. Then use the sort
function to sort the list in ascending order. Finally, call the procedure f for each integer
from 1 to 1000:
g := proc()
local M1;
begin
M1 := sort([op(M)]):
f(M1, matrixSize^2, i) $ i = 1..1000
end_proc:
Using the bisection method instead of accessing each matrix element significantly
improves the performance of the example:
time(g())
3724.233
Typically, the best approach is to use the appropriate MuPAD functions whenever
possible. For example, to improve performance further, rewrite the code using the
MuPAD function has. Also, converting a matrix to a set can reduce the number of
elements. (MuPAD removes duplicate elements of a set.) In addition to speed up, this
approach makes your code significantly shorter and easier to read:
g := proc()
local M1;
begin
M1 := {op(M)}:
has(M1, i) $ i = 1..1000
end_proc:
In this case, execution time is even shorter than for the code that implements the
bisectional method:
time(g())
1508.094
5-98