homechevron_rightStudychevron_rightMath

# Tips and tricks #8: How to reuse existing calculator

#### Tips and tricks series

This article relies on knowledge you should get from previous articles, so you may want to check them first.

### Algorithm reuse

In this article we will create the calculator which will use calculator already existing on site. Technically, our calculator delegates part of calculations to another calculator or reuse another calculator's algorithm.

As example, we will create calculator which will be just enhanced version of Probability of given number success events in several Bernoulli trials calculator. That calculator outputs probability only for single entered k. Our calculator will display probabilities in a table, for each k from zero to n (number of Bernoullu trials).

Create new calculator. Add two inputs:

Name Variable Type Default value Note
Number of Bernoulli trials n Number 10
Success probability p Number 0.5 Set "Allow decimal digits", set "Number range", set range from 0 to 1

Create output table. Hover table_chart Table button and click on Add output table menu item. This will bring up the output table editor dialog.

Fill Name and Variable with Bernoulli trials and table values respectively. Leave Description empty. The Table columns part of the dialog consists of two parts: column editor on the top and list of columns at the bottom, below button Add. For new table, it is empty.

Now fill the information for the first table column like this:

Field name Value Meaning
Variable k Name of Javascript variable, which is used to hold input value
Column Name Number of successes Column name as displayed in column header
Column appearance Show on diagram Marks this column as x-axis of the chart
Type Number The type of the output

Leave all other fields with their default values and press Add to add second column.

Fill the information for second table column

Field name Value Meaning
Variable p Name of Javascript variable, which is used to hold input value
Column Name Probability Column name as displayed in column header
Column appearance Show as line Marks this column as source of line chart
Type Number The type of the output
Number of decimal digits 3 number of decimal digits displayed in this column by default

Leave all other fields with their default values and press Add to add second column.

Note how list of columns now displays added columns. You can edit and delete it if you need to, using links in the list.

Press OK to close output table editor.

Now we need to add code into Calculate function.

Let's first create code to populate table.

for(var i = 0; i &lt;= n; ++i) {
rec.k = i;
rec.p = 0.0;
}

Note how resulting table now filled with zeroes

At this moment, we need to reuse existing calculator. To do this, click Algorithm button on toolbar. In search window, enter "Probability of k" and click Search. Click on search result (it should be "Probability of k success events in n Bernoulli trials").
This will generate sample call in your code

//Probability of k success events in n Bernoulli trials
var result = Planetcalc.Calculate4145( {
//-----  Inputs  -----
"p":0.2 //Event probability (Number)
,"n":10 //Number of independent trials (Number)
,"k":2  //Number of success events (Number)
} );

//-----  Outputs  -----
//result.P   - Probability (Number)

Insert it inside cycle. Finally your code should look like this

for(var i = 0; i &lt;= n; ++i) {
var result = Planetcalc.Calculate4145( {"p":p, "n":n,"k":i} );
rec.k = i;
rec.p = result.P;
}  PLANETCALC, Tips and tricks #8: How to reuse existing calculator