cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Need insite on compililng functions

wayne
3-Visitor

Need insite on compililng functions

I am seeking insight on the following:
I believe Mathcad compiles as it goes through a worksheet.

But functions (or programs) that are used over and over, are they compiled only once and reused?

Functions defined in a referenced file; are these compiled when the file is opened, or when the referenced file is come across in the worksheet?

And, as above, are the functions in a referenced file compiled only once or every time used.

I link long files together by referencing the previous file into the current one. The file takes a lot longer to open, but does not need to re-calculate what is in the referenced file, so the current file recalculates quickly. Is it correct to say that the referenced file is compiled and is static?

Thanks in advance for my educate
Could someone please educate me?
Thanks
13 REPLIES 13

A very good way to view referenced functions is the 5 MB "Creating Amazing Images in Mathcad". From recollection, there are only two sheets in reference but 100's of functions definitions, very quick. A must for best using Mathcad in the sense of referenced functions.

You have lots of questions .

jmG

Thank you jmg,
I am down loading the reference.

I have a lot of questions, but I think a simple explanation is all that is required ( I am not a programmer ). If I write a function or program, and then re-use that function again and again, does Mathcad compile only once, or does it go back to the original definition every time and compile again?

On 4/22/2010 1:47:28 PM, waynereid wrote:

>If I write a function or program, and then re-use that function again and again, does Mathcad compile only once, or does it go back to the original definition every time and compile again?

Go back and compile again. Just define a constant like a = rnd(2), save and insert this file as reference, you can check that each time that you insrt the reference, the value of a changes.

If you want a function without recompilation, can write an user dll.

Regards. Alvaro.

Thank you Alvero,

I might be a little thick and I apologize if I am.

I understand that DLL's compile when Mathcad is opened, and so the function would be fast.

I also understand that every time I insert a referenced file, that file would need to compile, just as if it was cut and pasted into the region each time.

I will try this:
If I wrote a series of calculations that I needed to perform over and over in a given work file, I could:
1) Do the calculations and cut and past every time I used them. Then they would compile every time used.
2) I could write them as functions or a programs the first time, then just use the previously defined functions. Does Mathcad keep a compiled version of the functions in memory. If so, it would be faster that doing 1).
3) I could put the function/programs in a file that is referenced in. I know that referenced files are static (they do not update unless re-inserted or the file is closed and opened again).
But is the referenced file in memory in compiled form?

Thanks for your patience
Wayne

You are not likely to get a definitive answer. What you are asking is about the internal implemenation of Mathcad. Those who actually know are in no position to talk about it. But one can speculate.

DLLs are compiled completely separately from Mathcad, long before it is even opened. They are fast because they are compiled into machine language -- and because they are very limited in what they can do.

There is some compilation that occurs in Mathcad. I don't know what form the compiled code is in. There is still a heavy amount of interpretive processing involved in evaluation -- the compile form might be some internal form run throug an interpreter, or it could be machine language, but mostly calls to various library functions. Makes little difference.

I expect that compilation occurs as part of parsing the expressions. Once. A referenced work sheet is treated the same as if the included regions were directly in the enclosing sheet. The referenced file is not maintained in memory as a file, only as a set or regions in the active worksheet.

Don't use cut and paste to repeat calculations. It's slow (not the compilation, which you probably can't even notice, but the mouse actions for doing the copy and paste. It's also prone to introduce errors, and makes it extremely difficult to modify the sheet (to change an expressiion you have to find and change all copies of it).
__________________
� � � � Tom Gutman
StuartBruff
23-Emerald II
(To:TomGutman)

On 4/22/2010 9:26:57 PM, Tom_Gutman wrote:
== There is some compilation that occurs in Mathcad. I don't know what form the compiled code is in.

In M11..M14, Mathcad compiles to mpl (Mathcad Programming Language?), a functional language. Have a look in your Mathcad folder for .mpl files to see what the code looks like. I have suggested that PTC look into the feasibility of issuing some updates to Mathcad by releasing .mpl files (eg, your Jacobian routines) rather than by updating the .exe or .dll files.

I have a vague feeling that Mathcad might do incremental compilation, but I don't have any hard evidence to support that feeling.

Stuart

Many collabs plug the required functions in a block, collapsed at the top of the work sheet called "Utilities". It works very well and as fast as it can be, w/o writting *.DLL. Most if not all Mathcad e-books have a referenced sheet at the top of the sheet, it seems to work the same way. Mathcad does not compile, it constructs the kernel the first time a work sheet is opened. It goes down looking for global definitions. If you have long work sheets and you want part(s) of the sheet to be exportable to another sheet for design, you might find that grouping all what's needed is very handy. Eventually, Mathcad 11 for instance does not like long work sheets. I have some > 80 pages ... that takes 45 seconds to calculate with plenty of solvers. In this case, as they are master collection, the top of the sheet is organised in such a way that the first page in view [the page on screen] does little maths or none, then I scroll down in one shot, waiting for the last calculations. This technique constructs the kernel once for ever for that worksheet in the best and fastest way. Anywhere in the sheet, I can call elementary functions like sin(1.23456789), Mathcad does not care because it does not calculate elementary functions, Pentium does ... Mathcad does not have to take a ticket, just an "hello Pentium, sin(1.23456789)". I call those things "echo functions".
I'm writting you a long image in words that mean something imagable in the mind, because I have no time to write it shorter in pedant words that not necessarily correspond to the Mathcad structure.

jmG

... eventually, if I would be your client, I would reject the referenced sheet technique because of the need to see all the project in a single document, especially if the "referenced material" contains : units system, conversion, constants, source info ... etc.

jmG

On 4/22/2010 6:24:20 PM, waynereid wrote:

>I understand that DLL's
>compile when Mathcad is
>opened, and so the function
>would be fast.

Are just precompiled code, but are read only once for all mathcad worksheets in a session.

I ask as a suggestion to make avaible the definitions in any mcd file under userefi directory to all the others worksheets, without recompilations

http://collab.mathsoft.com/read?130050,12

That provides the same user functions avaibles to all worksheets without compiling each time that they are needed. Just like an user dll, but in mcd format.

>I will try this:
>If I wrote a series of calculations that
>I needed to perform over and over in a
>given work file, I could:
>1) Do the calculations and cut and past
>every time I used them. Then they would
>compile every time used.

>2) I could write them as functions or a
>programs the first time, then just use
>the previously defined functions. Does
>Mathcad keep a compiled version of the
>functions in memory. If so, it would be
>faster that doing 1).

Yes, it do.

>3) I could put the function/programs in
>a file that is referenced in. I know
>that referenced files are static (they
>do not update unless re-inserted or the
>file is closed and opened again).
>But is the referenced file in memory in
>compiled form?

Yes, it is.

Regards. Alvaro.

To all, thank you.

FYI, this is what I do in my work:

I have extensive functions for design that are grouped in different files that I reference in for a particular project. All these files together would be (just guessing) 50 to 100 pages.

Doing this way makes the initial file slow to open, but then recalculation of the work sheet as I design is fast.

When a the working file gets to 30 to 40 pages, the recalculation time becomes a problem, so I start a new file and reference in the previous one. Again, takes a longer to open, but after that, recalculation is fast.

I build the design files as I go, so recalculation time if very important. I also need access to previous results (like beam reactions) that I use load other beams.

I agree with a previous comment that if I have a long file that I need to make changes to, I open and go to the bottom so that the whole sheet calculates and then go back through.

I was hoping to be able to provide a clear, concise, technical answer as to why this works the best for me, But looks like not so easy.

Wayne

On 4/23/2010 11:32:18 AM, waynereid wrote:
>To all, thank you.
...
>I agree with a previous
>comment that if I have a long
>file that I need to make
>changes to, I open and go to
>the bottom so that the whole
>sheet calculates and then go
>back through.
>
>I was hoping to be able to
>provide a clear, concise,
>technical answer as to why
>this works the best for me,
>But looks like not so easy.
>
>Wayne
____________________________

You got the point.

CAS don't compile in the sense of like FORTRAN IV [1970] did, therefore compile is the wrong word. At best, Mathcad like Mathematica creates the "kernel", collecting the functions. The other point is that the functions aren't necessarily executed in IPN and Mathcad versions based on Maple did organise the suite of calculations, in some sort then : compiled. MuPad is another gear that may not do the same "pseudo-compile".

However, read more.

Compile is useful in situations where you have to evaluate a particular numerical or logical expression many times. By taking the time to call Compile, you can get a compiled function which can be executed more quickly than an ordinary Mathematica function.
For simple expressions such as x Sin[x], there is usually little difference between the execution speed for ordinary and compiled functions. However, as the size of the expressions involved increases, the advantage of compilation also increases. For large expressions, compilation can speed up execution by a factor as large as 20.
Compilation makes the biggest difference for expressions containing a large number of simple, say arithmetic, functions. For more complicated functions, such as BesselK or Eigenvalues, most of the computation time is spent executing internal Mathematica algorithms, on which compilation has no effect.

Even though you can use compilation to speed up numerical functions that you write, you should still try to use built�in Mathematica functions whenever possible. Built�in functions will usually run faster than any compiled Mathematica programs you can create. In addition, they typically use more extensive algorithms, with more complete control over numerical precision and so on.
You should realize that built�in Mathematica functions quite often themselves use Compile. Thus, for example, NIntegrate by default automatically uses Compile on the expression you tell it to integrate. Similarly, functions like Plot and Plot3D use Compile on the expressions you ask them to plot. Built�in functions that use Compile typically have the option Compiled. Setting Compiled -> False tells the functions not to use Compile.

Compile works by making assumptions about the types of objects that occur in evaluating the expression you give. The default assumption is that all variables in the expression are approximate real numbers.
Compile nevertheless also allows integers, complex numbers and logical variables (True or False), as well as arrays of numbers. You can specify the type of a particular variable by giving a pattern which matches only values that have that type. Thus, for example, you can use the pattern _Integer to specify the integer type. Similarly, you can use True | False to specify a logical variable that must be either True or False.

The types that Compile handles correspond essentially to the types that computers typically handle at a machine�code level. Thus, for example, Compile can handle approximate real numbers that have machine precision, but it cannot handle arbitrary�precision numbers. In addition, if you specify that a particular variable is an integer, Compile generates code only for the case when the integer is of �machine size�, typically between a.
When the expression you ask to compile involves only standard arithmetic and logical operations, Compile can deduce the types of objects generated at every step simply from the types of the input variables. However, if you call other functions, Compile will typically not know what type of value they return. If you do not specify otherwise, Compile assumes that any other function yields an approximate real number value. You can, however, also give an explicit list of patterns, specifying what type to assume for an expression that matches a particular pattern.

The idea of Compile is to create a function which is optimized for certain types of arguments. Compile is nevertheless set up so that the functions it creates work with whatever types of arguments they are given. When the optimization cannot be used, a standard Mathematica expression is evaluated to find the value of the function.

The compiled code generated by Compile must make assumptions not only about the types of arguments you will supply, but also about the types of all objects that arise during the execution of the code. Sometimes these types depend on the actual values of the arguments you specify. Thus, for example, Sqrt[x] yields a real number result for real x if x is not negative, but yields a complex number if x is negative.
Compile always makes a definite assumption about the type returned by a particular function. If this assumption turns out to be invalid in a particular case when the code generated by Compile is executed, then Mathematica simply abandons the compiled code in this case, and evaluates an ordinary Mathematica expression to get the result.

An important feature of Compile is that it can handle not only mathematical expressions, but also various simple Mathematica programs. Thus, for example, Compile can handle conditionals and control flow structures.
In all cases, Compile[vars, expr] holds its arguments unevaluated. This means that you can explicitly give a �program� as the expression to compile.

If you make a definition like f[x_] := x Sin[x], Mathematica will store the expression x Sin[x] in a form that can be evaluated for any x. Then when you give a particular value for x, Mathematica substitutes this value into x Sin[x], and evaluates the result. The internal code that Mathematica uses to perform this evaluation is set up to work equally well whether the value you give for x is a number, a list, an algebraic object, or any other kind of expression.
Having to take account of all these possibilities inevitably makes the evaluation process slower. However, if Mathematica could assume that x will be a machine number, then it could avoid many steps, and potentially evaluate an expression like x Sin[x] much more quickly.
Using Compile, you can construct compiled functions in Mathematica, which evaluate Mathematica expressions assuming that all the parameters which appear are numbers (or logical variables). Compile[aa, a, � a, expr] takes an expression expr and returns a �compiled function� which evaluates this expression when given arguments a, a, � .
In general, Compile creates a CompiledFunction object which contains a sequence of simple instructions for evaluating the compiled function. The instructions are chosen to be close to those found in the machine code of a typical computer, and can thus be executed quickly.

One of the common motivations for converting Mathematica expressions into C or Fortran is to try to make them faster to evaluate numerically. But the single most important reason that C and Fortran can potentially be more efficient than Mathematica is that in these languages one always specifies up front what type each variable one uses will be�integer, real number, array, and so on.
The Mathematica function Compile makes such assumptions within Mathematica, and generates highly efficient internal code. Usually this code runs not much if at all slower than custom C or Fortran.

It is important to realize that since Mathematica can only sample your function at a limited number of points, it can always miss features of the function. By increasing PlotPoints, you can make Mathematica sample your function at a larger number of points. Of course, the larger you set PlotPoints to be, the longer it will take Mathematica to plot any function, even a smooth one.
Since Plot needs to evaluate your function many times, it is important to make each evaluation as quick as possible. As a result, Mathematica usually compiles your function into a low�level pseudocode that can be executed very efficiently. One potential problem with this, however, is that the pseudocode allows only machine�precision numerical operations. If the function you are plotting requires higher�precision operations, you may have to switch off compilation in Plot. You can do this by setting the option Compiled -> False. Note that Mathematica can only compile �inline code�; it cannot for example compile functions that you have defined. As a result, you should, when possible, use Evaluate as described in Section�1.9.1 to evaluate any such definitions and get a form that the Mathematica compiler can handle.

jmG




Thanks, jmg

Some of that I actually get. By the way, Life was simpler when FORTRAN was new.

Wayne

On 4/23/2010 2:20:37 PM, waynereid wrote:
>Thanks, jmg
>
>Some of that I actually get.
>By the way, Life was simpler
>when FORTRAN was new.
>
>Wayne
_____________________________

As you can see, Mathematica has a Compile function, but it almost does nothing unless you know in advance the application and how it will behave. On the examples given, you mostly get a blue page of "Not a machine number". Considering CAS [Computer Algebra System] like Mathcad for the Windows OS, compiling would mostly mean execute in parallel, and that is not possible. Only big blues which are "Automata machines" do parallel computation. In my time, some mini computers like Modcomp, Digital ... were running in pipe line architecture, they compiled once for all, whereas they were finally doing nothing else than the programme(ssss). They would do no more maths than the ones demanded, frozen in machine language after compiled. I believe there is NO possibility to code a Mathcad function in machine language, i.e: compile.

In some case of functions that demand a lot of elementary functions like exp[ln(u)/n], you might think of trying to rewrite the function as a Cheby polynomial, that you can truncate. This example function will gain nothing whereas the function in Cheby equivalent will have about the same number of arithmetic operations and much slower because not able to code in machine language as it is in the Pentium. This kind of dream is circular. Often, there are ways to structure a work sheet. An example is the Minerr based on SSE that might take lot of minutes in comparison to the PWMinerr in few seconds.

Interesting [jmG]
Top Tags