logo

Swift Calcs BlogNews, notes, and ruminations from the Swift Calcs team

What's New for May: Programs and Multi-line Functions

A new month means new features at Swift Calcs, and today we are excited to announce the availability of multi-line functions, which we call programs, in Swift Calcs.

Functions

Often times when performing an engineering calculation, we have create a set of steps to determine an answer based on one or more inputs. When we can simplify this down to a single line, we can use a function in Swift Calcs.

But what if we need more than one line? Swift Calcs has allowed conditional piece-wise functions since December, but any other multi-line functions or structures were not possible, until now.

Introducing Programs

Multi-line functions in Swift Calcs are called programs. With a program, a set of Swift Calcs commands are pre-compiled before use, greatly speeding calculation time and allowing for complex calculations to be repeated for a number of inputs or settings.

Programs allow all normal Swift Calcs commands that set the value of variables or functions, including math mode, solvers, and other tools from the toolbox. Chain commands together just like in a worksheet, and return your value for later use.

An Example

The easiest way to understand programs is by looking at an example. Consider finding the solution to a cubic equation in Swift Calcs:

image

What if we wanted to know the solution for a number of cubic equations with a differing cubic term coefficient? Before programs, we would have to copy the solver line multiple times, and update it each time for the value of the coefficient we want to solve for.

Programs provide another way. By using a program, we can wrap the solver into a function definition, and return the value:

image

Here, we have defined a program, cubic, with a single input, A. Inside the program, we utilize the solver, which depends on A, to find the solution near -1 to our cubic equation, and store it in a local variable, result. We finally use the return statement to return the value in result to the function.

On the next lines, we call the cubic program like we would any other function, and the solution is immediately returned. You can use programs anywhere like you would a function, even in plots...let's plot the cubic program for input values from 4 to 8:

image

Or use the solver to find the cubic coefficient that has a root at -1.35:

image

Programs can have any number of inputs, and any number of internal lines, to produce a single output. Multiple outputs can be returned as an array. Flow control, including if statements and for loops, allow you to add complexity and logic to your programs. And like any other function or variables, whole programs can be included in to other worksheets using the include worksheet tool.

Getting Started

To create a program, click and drag the program tool from the Programming section of the toolbox on to your worksheet. Alternatively, you can type program followed by a space on a new math-mode line.

Need more information? Check out the help article on programs and our new example on multivariate hypergemoetric distributions to see a complex program in action.

What's New: Include Variables from Other Worksheets

We've been spending the last weeks hard at work adding new functionality to the Swift Calcs platform and are excited to today announce the first major feature from this effort: the ability to include variables and functions into a worksheet.

Often times when performing calculations, it is useful to segregate different aspects of a calculation into multiple worksheets, or to define different inputs in different locations. Starting now, you can include variables or functions into a Swift Calcs worksheet that are defined in another worksheet. Get started by clicking the Import Worksheet tool under the General tab in the toolbox. You can include variables from any worksheet to which you have read level access, whether you are the original author or whether the worksheet was shared with you.

For users on our Free Public plan, you can include up to 2 variables or functions per source worksheet. For our Paid members, there are no limits on the numbers of variables or sources you can include in your work.

But wait, there's more...

We weren't done there, however. In addition to including your own worksheets, Swift Calcs has built a curated library of worksheets complete with useful constants and functions for a variety of engineering and scientific fields. We are starting today with constant libraries for the following fields:

When using the Include Worksheet tool, you'll be given the option of loading from the Swift Calcs library. We are working hard to increase the number of curated worksheets available over and plan to release more over the next few months, so keep checking back!

And we still weren't done

Often, its useful to define your own standard set of constants or functions for use in your work. Swift Calcs also allows you to set a default set of worksheets and variables to include in every new worksheet you create, ensuring your library of information is always automatically loaded for you. Set up your own defaults by clicking Change Settings under the account menu at the top right, or by clicking Change Default Settings under the Options menu in the menubar.

More help is available

Learn more about this feature and how to use it in the Swift Calcs help articles.

Hot off the press: Plots and Charts with Plotly.js

Our graphics team is happy to announce that Swift Calcs plots and charts are now driven by the wonderful open source library plotly.js!

Built on d3.js, plotly.js offers a wide range of chart types, visual styles, and display options that enable more refined and visually pleasing plots and charts in Swift Calcs. This new library is a great improvement over the previous library we relied on, bringing with it great cross-browser support, stability, and a wide range of options.

And what does this mean for me?

As of today, Plotly has been integrated into Swift Calcs as a true drop-in replacement. Your plots, including styling options for markers, lines, and colors, will now appear in Plotly generated diagrams. The plot types have been simplified, and more display options are available, including:

More plots are next...

The change to Plotly also opens a wide range of new plot options to the Swift Calcs development team, and we are working hard to enable these new plot styles for general use. Soon, Swift Calcs will support contour plots, interactive 3D wireframe and surface plots, bubble plots, pie charts, and more.

So here's a thanks to Plotly.js and the wonderful open-source software community for powering the graphics back-end that makes Swift Calcs possible!

What's New: February 2017

Celebrate Valentine's Day with these new features from Swift Calcs...you'll fall in love with math all over again!

Unit Pre-Processing and Adaptive Plots

As outlined in an extensive blog post, we increased the speed of solvers and plots by 5-100x through the use of unit pre-processing and adaptive plotting. The chart below shows computation times for some of our test scripts before and after this update...it's made an incredible impact! Head over to the blog for a full rundown.
image

Variable Accents

An often requested feature, Swift Calcs now supports adding accent characters to variable names, including single, double, and triple dots, tilde, bar, and the vector symbol. Find the new accents in the math toolbar!
image

Checkbox, Radio, and Pulldown Inputs for Variables

The slider is one of the most-used tools in the Swift Calcs toolbox, providing a visual and interactive way to set the value of a variable for use later in a worksheet.

We have now added three more interactive entry tools, all available in the Variables section of the toolbox:

Spline interpolation

A new interpolate tool has been added to the toolbox under the Data section. Provide a set of x and y data and the tool will return a piecewise function to interpolate values between points. The tool also allows you to choose the order of the splines (linear, quadratic, etc) and whether to allow extrapolation outside of the data domain.
image

Plot Auto-Range

For function plots without pre-set y-axis limits, we've implemented a new sampling algorithm to determine appropriate limits to choose in order to produce a visually useful plot. Previously, we would simply use the maximum and minimum values for the plot, a strategy that fails when asymptotes are present in the plot. Take a look below to see how the plot of cos(x)/x appears before and after the update:

image image

Tooltip help

Looking to learn keyboard shortcuts? We've added tooltips to all toolbox tools and menubar items. Simply hover your mouse over a tool or menubar option, and after a short moment a small tooltip will appear with the text shortcut. For tools, you can type the text shortcut at the start of any math mode item to insert that tool, and menubar shortcuts can be used anywhere inside a math mode item.
image

Convert to PDF

The best way to transform a Swift Calcs document into a PDF is by using the 'print to PDF' feature available on many computers. Not everyone has this option, however, or it may not be available on your machine. To support conversion to PDF, we've added a Download as PDF option in the File menu. When chosen, the system will generate a PDF of the current worksheet and email it to you as an attachment.

A set of functions for finding values in vectors

Check out three new functions in the command library: closest, closestgreater, and closestlesser, which will return the closest value in a vector to the desired number.
image

Team Management

With the release of Swift Calcs Professional and Business, we are now offering support for teams. When you subscribe to a pro or business membership for your team, the team administrator will gain the ability to add or remove users from their team, and users on the team will have a new sharing option to share whole Projects or Worksheets with members of the team.

New Worksheet Listings and Sort by Name

We've updated our default worksheet listings. You'll now see in Swift Calcs your active worksheets by default, which includes all worksheets you have opened or edited, regardless of whether you created them. You'll also find in the Project list a section titled Created by Me that lists worksheets and Projects you created. If others have invited you to worksheets, you'll see a Shared with Me section, and teams will see Team Projects as well. These new project lists help organize your work and make finding projects and worksheets faster and easier. Finally, we've added support for sorting the worksheet listing by name, as well as the usual date, to help in finding files.

iOS external keyboard support

Do you use an iPad with the Smart Keyboard or an external bluetooth keyboard with any iOS device? There is a weird bug where Apple devices don't send non-alphanumeric key presses to the browser window (why? Because Apple). This made Swift Calcs nearly unusable on these devices. Using a workaround described here, we can discern keypresses on iOS by examining the change in a textbox when non-alphanumeric keys are pressed on iOS devices. The end result? External keyboards with iOS now work as expected, opening up a new range of devices to the Swift Calcs family.

Unit Pre-Processing and Adaptive Plots

Noticed speed improvements in Swift Calcs plots and solvers recently? It's not your imagination: Quietly slipped in to a weekly production release two weeks ago was an update to our mathematics engine that increases calculation speeds up to 100x, depending on the calculation.

The secret to the bump? Well, its actually two:

Unit pre-processing

Working with units is a huge benefit of performing calculations with Swift Calcs, but the downside is that the inclusion of unit operators adds considerable overhead to each calculation. Units of the same type have to be converted during operations, checks have to be made to ensure unit consistency, and unit simplifications have to be done to remove canceled dimensions.

For most calculations, this added overhead is hardly noticed, but for operations that require continual re-evaluation of a function or expression, such as solvers and plots, this added overhead turns in to a real drag.

In these situations, however, we don't really need to do all the extra unit calculations on each and every iteration. We really need to do it once, up-front, to check for unit collisions or inconsistencies. Once we know the units are valid, we can simplify the expression by converting all units to mksa equivalents and then stripping them out.

In this way, we reduce the unit calculation overhead considerably by removing it from internal iterative routines. When we get our results, we simply re-apply the output unit to the answer. The results of this approach are dramatic...take a look at measured computation times for some functional tests before and after the change:
image For some calculations involving heavily nested functions with lots of unit calculations, this approach increased computation speed nearly 100x! And best of all, it works in the vast majority of situations...but when it doesn't (for example, when non-absolute temperature scales are utilized...ugh offsets!), we can simply revert to the previous method to perform the calculation.

Adaptive Plotting

When it comes to plots, however, we have more to share: Prior to last week's release, Swift Calcs used a plotting algorithm that is best described as brute-force. We would take a plot window, determine the number of pixels along the x-axis, and calculate the corresponding y value for each location. That equals 590 calculations for each function plotted. Consider y=x2 below using our old implementation...the number of sampled points is so large you can't really discern one point from the next:
image

This is overkill. Most functions don't need every pixel to be calculated to produce a visually accurate line. To reduce computational load, our new routine focuses on areas of high-curvature. We use an adaptive sampling routine that starts with a certain number of points (we use 20) roughly evenly spaced throughout the plot domain (not exactly even as we apply a bit of randomness to each point...if we didn't, we could get tripped up by periodic functions that have the exact same frequency as our sample points).

Next, we take a look at each sampled point and its two neighbors. If the angle formed by the three points is between 170 and 190 degrees, we have minimal curvature and no further refinement is needed (Tangent: The angle calculated is the perceived angle when plotted on the screen, as opposed to the true angle between the vectors. So its value depends on both the x-axis and y-axis limits, as well as the plot aspect ratio and use of log-plots). If we're outside these bounds, we calculate another point and at it to our sample, and again run the curvature comparison.

In this way, we continue to refine the plot until all sampled points are within the curvature bounds, or we've reached the resolution of the screen. Take a look at y=x2 plotted using our new method:
image

And here are the two plots from before and after, with the sampling points removed to only show the functional form:

image image

The plots are visually indistinguishable (can you tell which is which?), but the number of calculations (and therefore the time) to produce the one on the left is over 10 times less.

Most functions we tested end up using somewhere between 60 to 120 points, representing speed improvements of 3-10x from our previous brute force method. Combined with the unit pre-processing above, we're seeing speed improvements of between 10-100x...plots now often render in fractions of a second!

And added bonus: Dynamic Range

A nice benefit to the inclusion of the adaptive sampling algorithm is that a dynamic range solver comes along for free.

What is dynamic range? This is the idea that when producing a function plot where the y-axis bounds we're not set to specific values, the plotting engine should return appropriate y-axis bounds that produce a visually useful plot. For most functions, this range is simply the minimum and maximum y-values observed within the plot domain. However for plots with asymptotes that approach infinite values, the asymptote spike leads to an extremely large maximum or minimum value that create a visually useless plot.

Our calculation of the visual angle for the adaptive sampling technique, however, requires us to 'know' the y-axis range to determine the angle. Using this information, we can apply a simple test: Every time we add a new point in the sampling routine, if the new point is outside the currently known y-axis bounds, we consider the point to be 'outside' the preferred range if it would cause the value of:
image to increase by a factor of 8 times or more. Otherwise, we simply increase our known y-axis bounds. This helps us ignore asymptotes when a plot blows up and focus on the portion of the function of interest. Take a look at the plot for y=cos(x)/x from before and after this change:

image image

Clearly the new plot is more useful than the old.