Message Boards Message Boards

How to write a time efficient user-defined function with local variables?

Posted 5 years ago

I want to write a 'Gauss' function in my main program like the way its written in Matlab. In the following example I want to pass the variables ('aar' & 'es' are matrices and other are scalars) "aar, es, x1value, x2value, x3value, x4value, y1value, y2value, y3value, y4value" like given below. I want the output to store in detjacobs & Invdetjacobs. Kindly suggest me the correct way.

[detjacobs, Invdetjacobs] = Gauss[aar, es, x1value, x2value, x3value, x4value, y1value, y2value, y3value, y4value]


Do[
Do[
r = aar[[i]]; s = es[[j]];

shape1 = ((1. - r) (1. - s))/4.; shape2 = ((1. + r) (1. - s))/4.; shape3 = ((1. + r) (1. + s))/4.; shape4 = ((1. - r) (1. + s))/4.; dhdr1 = 1./4. (-1. + s); dhdr2 = (1. - s)/4.; dhdr3 = 1./4. (1. + s); dhdr4 = -(1. + s)/4.; dhds1 = 1./4. (-1. + r); dhds2 = 1./4. (-1. - r); dhds3 = (1. + r)/4.; dhds4 = (1. - r)/4.;

 detjacobs = {{dhdr1 x1value + dhdr2 x2value + dhdr3 x3value + 
  dhdr4 x4value, 
 dhdr1 y1value + dhdr2 y2value + dhdr3 y3value + 
  dhdr4 y4value}, {dhds1 x1value + dhds2 x2value + dhds3 x3value +
   dhds4 x4value, 
 dhds1 y1value + dhds2 y2value + dhds3 y3value + dhds4 y4value}};

Invdetjacobs = 1/detjacobs;

,{j,1,4}];

,{i,1,4}];
Attachments:
3 Replies

Take a look at the documentation for Module and Block for defining local variables. If evaluation speed is important, see https://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/. Hope this helps, Ian

POSTED BY: Ian Williams

Thanks Mr. John. This time I uploaded the notebook file. Kindly look into it. I have so many local variables. Will "Block" and" Return" commands consume more time? Actually, I am trying to write a time-efficient program.So, I thought of using this function in between to minimize time. In Matlab, there is room to define local variables while maintaining time efficiency. How to doit in mathematica?

Anonymous User
Anonymous User
Posted 5 years ago

Mathematica has at least a few ways to do it.

I assume you already looked in Help and found your answers. And I'm unsure what your Guass function is (what book what equation) (there must be many guass equations).

I do know in the old Mathematica book it could be hard to find the right section in Help (if one looked up Function one got a lesson on that keyword). However in the new Help there are tutorial related guides and suggested other readings: I assume you've already found your answer.

Value assignment:

{detjacobs, Invdetjacobs} = Gauss[aar_Integer, es_, x1value_, x2value_, x3value_, x4value_, y1value_, y2value_, y3value_, y4value_] := Block[ {i=1,j=1}, Do[ Do[ r = aar[[i]]; s = es[[j]],imax],jmax] ]

where the last expr evaluated is the return value (Return[value] can be used). I used Block because

 n=fun[x_]:=x+1;

does not include any facility for "local variables" which you may be used to (it actually DOES but that's a lesson on Context[]).

Delayed assignment (value is evaluated each time variable as a value appears)

{detjacobs, Invdetjacobs} := Gauss ...

Now note aar_Integer - it says this function is ignored if Guass is called with something other than. There can be many Guass each matching certain variable types or even certain patterned expressions.

POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract