Tips and tricks #8: How to reuse existing calculator
This article describes process of reusing another calculator's logic (algorithm) in your calculator.
Tips and tricks series
- Tips and tricks #1: How to set output value
- Tips and tricks #2: How to make calculator translatable into different languages
- Tips and tricks #3: How to make calculator with custom errors
- Tips and tricks #4: How to make calculator with table output
- Tips and tricks #5: How to show/hide input and output controls in a calculator
- Tips and tricks #6: How to make calculator with virtual keyboard
- Tips and tricks #7: How to make calculator with SVG image map
- Tips and tricks #8: How to reuse existing calculator
- Tips and tricks #9: Big numbers
- Tips and tricks #10: Files, long calculation mode
- Tips and tricks #11: Dynamic table columns
- Tips and tricks #12: Long (infinite) calculations
- Tips and tricks #13: Bitmaps and Pixel manipulation
- Tips and tricks #14: How to use a masked input
This article may rely on knowledge you should get from previous articles, so you may want to check them first.
Algorithm reuse
In this article, we will create a calculator which will use a calculator already existing on site. Technically, our calculator delegates part of calculations to another calculator or reuses another calculator's algorithm.
For example, we will create a calculator which will be just an enhanced version of the 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 Bernoulli 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 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 the 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 the 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 in the information for the 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 a 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 the second column.
Note how the 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 the output table editor.
Now we need to add code into the Calculate function.
Let's first create code to populate table.
for(var i = 0; i <= n; ++i) {
var rec = table.AddNewRecord();
rec.k = i;
rec.p = 0.0;
}
Note how the resulting table now filled with zeroes
At this moment, we need to reuse the existing calculator. To do this, click the Algorithm button on the toolbar. In the search window, enter "Probability of k" and click Search. Click on the search result (it should be "Probability of k success events in n Bernoulli trials").
This will generate a 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 the cycle. Finally, your code should look like this
for(var i = 0; i <= n; ++i) {
var result = Planetcalc.Calculate4145( {"p":p, "n":n,"k":i} );
var rec = table.AddNewRecord();
rec.k = i;
rec.p = result.P;
}
You are ready to publish.
Click on Preview button. By default, you should see a table and chart with different probabilities of k successes. If everything is working as expected, Publish the calculator. After publishing, calculator will receive persistent address and will be available for other users of the site. You can also embed it in articles. In our case, you can see it right below:
Kommentare