zlelik

145 Reputation

4 Badges

16 years, 60 days

MaplePrimes Activity


These are answers submitted by zlelik

I try to use Maple 12 and result is the same. Maple use only 1 processor (or 50% of each processor for 2-processor system) :(

May be on other operation system (Linux, MacOS and etc) I can use multiprocessor system for calculation with maple?

Thank you for all.

I try to use Compiler:-Compile( ):

But I get some errors

>M3 := (x) -> Array([[x^2, x], [1, x]], datatype=float[8]);

>M3(0.1);

[.100000000000000002e-1 .100000000000000004]
[ ]
[1.    .100000000000000004]

>cM3 := Compiler:-Compile( M3 ):
Error, (in printtab[CodeGeneration:-Names:-ArrayInitialization]) cannot translate procedures that allocate or return new rtables, lists or expression sequences

>f3 := (x :: float) -> M3(x)[1,1];

>f3(0.1);
                            0.0100000000000000002

>cf3 := Compiler:-Compile( f3 ):
Warning, the function names {M3} are not recognized in the target language
Error, (in printtab[CodeGeneration:-Names:-FunctionCall]) cannot determine the return type for call to function `M3'

Simple functions like in example below work correctly with Compiler:-Compile( ):

>f5 := (x) -> x*x + sin(x);

>cf5 := Compiler:-Compile( f5 ):
>cf5(0.4);
f5(0.4);
                            0.549418342308650496
                                0.5494183423
 

 In the Maple help for Compiler:-Compile( ) I read: "Multidimensional Arrays (such as Matrices) are supported."

Thank you. It works!

I thought, that if I write

f(0.1);

I calc on Maple floating point with given precision (Digits) and its equivalent

evalf(f(1/10));

End if I use

evalhf(f(0.1));

I use hardware floating point with precsion, given by hardware.

I try this example (like in Maple help)

p := proc( n :: posint, x :: float ) :: float;
  local i :: integer, s :: float;
  s := 0.0;
  for i from 1 to n do
    s := s + x / i
  end do;
  s
end proc:
>time(p(100000, 2.3));
                                    1.301
>time(evalf(p(100000, 2.3)));
                                    1.341
>time(evalhf(p(100000, 2.3)));
                                    0.150
 

evalhf in 10 times faster than simple using floats

The real task is more complicated. I want save you time and please, belive me, I can not avoid matrices and piecewise.

I use big theory, that built on matrices and if I want avoid matrices, I need build this theory from scratch.

Full descrption of task with matrices I write in the bottom of post.

Does exist any method to decrease time of plotting?

 

Full theory description (this is theory of field in Laser cavity):

I have collection of 4 basic matrices with complex elements (

Mmed(K0, dx, L, xi, k, z)
Mn(N)
Mp(p)
Mfs(z)

).

And I need find full matrix of system M as multiplication of basic matrices. Usually I need multiply 6-7 basic matrices in different order and with different arguments to get full matrix of system. Order of matrices depends on some variables (z and 2 other parameters of system geometry). I make about 6 full matrices of system for all combination of system geometry. Then I compose one full system matrices via piecewise and get one full matrix of system.

I do it, because main idea of using theory is compose full matrix system and then find some system characteristic from matrix elements.

I find 3 different functions (w, S, G) from full system matrix. For example

w = Re( (M[2,2] - M[1,1])/(2*M[1,2])+ I*sqrt((M[1,1] + M[2,2])/2))

M - full system matrix

Then I need analyze this 3 function by plot and use this functions in future calculations. Usually I need calculation integral of w, S, G, I need use w, S, G as function in differential equations, I need find w , S, G from direct full system matrix (w, S, G) and from inverse full system (w_i, S_i, G_i) matrix and find w-w_i, S-S_i, G-G_i.

If I do not use piecewise I should use 6 functions w, 6 function S and 6 function G and if I need plot full w function i need use piecewise in plot, or plot all 6 w functions on one plot. I don't know how can I insert 6 w function in differental equaton, also I have problem with finding integral for w function and so on.

All theory, that I use, based on finding one full system matrix and finding one w, one S and one G function. finding w, S, G is only begin of theory.

Thanks a lot, changing in piecewise make my program faster on 5-10%, but it very small benefit for me.

My real physical task is like in example below

M1 := (g) -> <<1 | 0>,<-2*(1 - g) | 1>>;
M2 := (z) -> <<1|z>,<0|1>>;
Ma := (z, g) -> M1(g).M2(z);
Mb := (z, g) -> M2(z).M1(g);
ww := (z, g) -> piecewise(z <= 0.5, Ma(z, g)[1,1] + Ma(z, g)[2,2], Mb(z, g)[1,1] + Mb(z, g)[2,2]);

ss := (z, g) -> piecewise(z <= 0.5, Ma(z, g)[1,1] - Ma(z, g)[2,2], Mb(z, g)[1,1] - Mb(z, g)[2,2]);

I need multiply some matrices in difference order depends on z value. And get some functions (like ww and ss in my example) from result matrix. I need possibility of changing only one matrix in one place, and than recalculate functions ww and ss. I want use matrices, because matrices have real physical meaning and rewrite code without matrix make code very unreadable.

I don't know, how I can change my code for using Compiler:-Compile.

I try to use Compiler:-Compile and get error:

cww := Compiler:-Compile( ww ):
Warning, the function names {piecewise, Ma, Mb} are not recognized in the target language
Error, (in printtab[CodeGeneration:-Names:-FunctionCall]) cannot determine the return type for call to function `Ma'
 

may be it will be easier to change my code for using evalhf? I try it and get error too

ww(0.3,0.4);
ss(0.3,0.4);
                                 1.640000000
                                0.3600000000
evalhf(ww(0.3,0.4));

Error, unable to evaluate built-in function `member` in evalhf
 

 

Can you tell me more about "your piecewise should not have a branch 'otherwise' (if you
want performance, write something like 0.5 <= z for example)."?

You mean that

piecewise(5 >= z, z, z*2)

work faster that

piecewise(z <5, z, z*2)

?

This sample is simplified full function. Full function look like w(K0, dx, xi, k, zh, h, z, zt, N, L, p) and some of variables is the constant and some of variables is zero. For ideal case I have w(z,g), and some piecewise and matrix may be not needed, but in general case in real system I have all w(K0, dx, xi, k, zh, h, z, zt, N, L, p) and I need increase performance of calculation of full function w(K0, dx, xi, k, zh, h, z, zt, N, L, p).

May be you can help me to understanding common performance increasing method in Maple? 

What is difference between fraction instead of floating points?

P.S.

I need only numerical calculation!

Very strange, but in Maple help about plot I see only samples like this

s := t->100/(100+(t-Pi/2)^8): r := t -> s(t)*(2-sin(7*t)-cos(30*t)/2):
plot([r(t),t,t=-Pi/2..3/2*Pi],numpoints=2000,coords=polar,axes=none);
 

or

plot(cos(x) + sin(x), x=0..Pi);

and not any information about function in apostrophe

plot('f(g)', g=0..1)

 

 

Thanks, it work in plot

thank, I will try it. may be I have another problem, because f(1) = infinity

w := (z,g) -> .1469061274e-2*(1/Im((piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[2,2]-piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[1,1])/piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[1,2]+2*I*((1-(1/2*piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[2,2]+1/2*piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[1,1])^2)/piecewise(z < .1950000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.1950000000-z)+.8050000000+.8050000000*z*(-2+2*g)+z],[-2.+2*g,(-2.+2*g)*(.1950000000-z)-.6100000000+1.610000000*g]]),piecewise(z < .2050000000,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.2050000000-z)+.7950000000+.7950000000*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.2050000000-z)-.5900000000+1.590000000*g]]),piecewise(z < .5,Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(.5-z)+.5+.5*z*(-2+2*g)+z],[-2.+2.*g,(-2.+2.*g)*(.5-z)+1.0*g]]),Matrix(2, 2, [[1.+z*(-2+2*g),(1.+z*(-2+2*g))*(1-z)+z],[-2.+2.*g,(-2.+2.*g)*(1-z)+1.]]))))[1,2]^2)^(1/2)))^(1/2);

 

f := (g) -> evalf[20](Int(w(z, g), z=0.0..1.0));

 

f(-0.2);

0.8042778884924786499363261e-3+0.*I


plot(f(g), g=0.0..1.0);

Error, (in signum/main) too many levels of recursion

 

It works for one floating-point calculation but does not work for plot.

Sorry, but it works, when I calculate one value, But it don't work, when I try to make plot :(

Thanks a lot!

It's Work!

 

also it's work when I change integration  method (evalf(Int(w(z, 0.3), z=0.0..1, method=_Gquad));)

but it don't work for more complex function. Your method works in all cases.

1 2 Page 1 of 2