Processing math: 100%
Reference manual for the PQcalc online calculator
PQcalc is a program for doing arithmetic with physical quantities (the PQ in PQcalc stands for physical quantities).
Physical quantities (i.e. measurements and data derived from measurements) have units associated with them, and most of
them are associated with a certain degree of uncertainty. PQcalc is able to calculate with units, and has a very simple
way of dealing with error propagation (i.e the question how uncertain a derived quantity is, given the uncertainty of the
quantities it is derived from).
This reference manual goes through the basic features and capabilities of PQcalc. Additional illustration of how to
use the program is available through the help button on the user interface. Clicking on one of the examples preloads commands
into the input field, and clicking the go button shows the results of the commands. For detailed information about the
way the program works, the source code is also available as a resource. This reference manual itself is structured roughly in
the order in which calculations are setup, run and interpreted, followed by special topics relevant for teaching with the tool. Each page
in the manual consists of a narrative, followed by a table showcasing commands input by the user on the left and program
output on the right that illustrate a group of features discussed in the narrative.
Entering quantities
The basic format of entering quantities is "name = number * unit". PQcalc keeps track of significant figures, e.g. the
2.74 is assumed to have an uncertainty of plus or minus 0.01. Numbers without decimal points are taken as exact, e.g. 2 mol.
Units made up of more than one basic unit can be written as products and fractions, or as units raised to the power of
a whole number, as shown in the examples below. If a name for a quantity is used a second time in a calculation, the old
value is overwritten.
t1 = 45.00 s |
t1=45.00 s
|
t2 = 45.0 s |
t2=45.0 s
|
t3 = 45 s |
t3=45 s
|
v = 3.4 m/s |
v=3.4 ms
|
v = 3.4 m s^-1 |
v=3.4 ms
|
R = 8.314 J/(mol K) |
R=8.314 JK mol
|
R = 8.314 J mol^-1 K^-1 |
R=8.314 JK mol
|
Meaningful names for quantities
Names for quantities have to start with a letter, and can't contain spaces (see exceptions below).
Otherwise, you can choose any combination of letters, numbers and special characters to name quantities.
There are some conventions to format names with subscripts and chemistry markup.
Subscripts are preceded by
an underscore, as in v_t. If the name starts with a single letter followed by digits, such as v0, the number is written
as subscript (i.e. you don't have to write v_0). Multiple underscores result in multiple subscripts separated by commas in the
marked-up version. Examples are in the top half of the table.
To show chemical formulae and equations with a minimum of typing, enclose them in brackets. If the name starts
with a bracket, they will be retained in the formatted version (this is a convention to write concentrations).
If the brackets occur later in a name, they are dropped, and what is inside them is formatted as subscript,
with chemical formulae and equations typeset by the mhchem package of LateX (link). Spaces are allowed within
the brackets. Examples are in the bottom half of the table.
v_t = 0 |
vt=0
|
v0 = 0 |
v0=0
|
v_x_y = 0 |
vx,y=0
|
[Ca^2+] = 0 |
[Ca2+]=0
|
m[H2O] = 0 |
mH2O=0
|
K[2NO2 <=> N2O4] = 1 |
K2NO2−⇀↽−N2O4=1
|
Units recognized by PQcalc
PQcalc knows about the international units m, s, kg, K, A, mol, Cd for length, time, mass, temperature, electrical current,
chemical amount, and luminous intensity. It also knows about metric prefices (smaller thant the original unit:
milli, micro, nano, pico; larger than the original unit: kilo, Mega, Giga). Moreover, it knows about common combined units
(such as N for force, J for energy, W for power). In addition, it has some non-SI units built
in (atm and mmHg for pressure, min and h for time, L for length cubed, M as an abbreviation of mol/L, eV for energy). All
of these units may be used to enter quantities.
When you do arithmetic with multiple quantities, PQcalc figures out the units of the result. Here are some examples.
t_total = 3 min + 5. s |
ttotal=3 min+5. s
=3.08 min
|
t_total using s |
ttotal=3.08 min
=185. s
|
v = 50. m / t_total |
v=50. mttotal
=50. m185. s
=0.270 ms
|
t_shorter = 100. s |
tshorter=100. s
|
d_shorter = v * t_shorter |
dshorter=v⋅tshorter
=0.270 ms⋅100. s
=27.0 m
|
Annotations
In order to document the what, why and how of a calculation, the arithmetic done by PQcalc can be annnotated through
comments written by the user. If you follow the suggested scaffold for solving a problem, it makes sense to comment on
the setup (what is the problem in words?), the solution (what are the steps in a multi-step problem), and the interpretation
of the results (is the answer in the expected range with the correct dimensions?). Comments that start with a pound sign (#) are copied verbatim into the
output of PQcalc unless they are surrounded by brackets (chemistry markup) or preceded by an underscore (markup like the
name of a quantity). Comments that start with an exclamation mark (!) are shown in larger font with chemistry markup, and
are conveniently used to show balanced chemical equations.
# This is a comment |
This is a comment
|
# Spaces in comments are retained |
Spaces in comments are retained
|
# The quantity _v_0 refers to the initial velocity |
The quantity v0 refers to the initial velocity
|
# The formula of water is [H2O] |
The formula of water is H2O
|
! 2H2 + O2 -> 2H2O |
2H2+O2⟶2H2O
|
Performing calculations
Calculations are entered in the general format "calculated quantity = operations on defined quantities". You can add ("+"
and subtract ("-"), multiply ("*") and divide ("/"), and raise to a power ("^"). Moreover, you can apply functions to quantities
as described in the next section.
t_a = 0.05 s
t_b = 52.100 s
dist = 4.52 m |
ta=0.05 s
tb=52.100 s
dist=4.52 m
|
t_diff = t_b - t_a |
tdiff=tb−ta
=52.100 s−0.05 s
=52.05 s
|
t_ratio = t_a / t_b |
tratio=tatb
=0.05 s52.100 s
=1×10−3
|
v = dist / (t_a + t_b) |
v=distta+tb
=4.52 m0.05 s+52.100 s
=4.52 m52.150 s
=0.0867 ms
|
Functions
PQcalc supports a limited set of functions. They were chosen to cover the basic needs of a General Chemistry course.
It is trivial to add other functions, especially if they are already available in python's math package.
The following functions accept a dimension-less argument and result in a dimension-less value:
- exp(q): exponential function
- ln(q): natural logarithm
- log(q): base 10 logarithm
- sin(q): sine of radian argument
- cos(q): cosine of radian argument
- tan(q): tangent of radian argument
The following functions accept arguments with dimensions.
- minimum(q1, q2, ..): minimum of a list of quantities
- maximum(q1, q2, ..): maximum of a list of quantities
- sumover(q1, q2, ..): sum of a list of quantities
- average(q1, q2, ..): average of a list of quantities
- quadn(qA, qB, qC): yields one solution of the quadratic equation Axx + Bx + C = 0
- quadp(qA, qB, qC): yields the other solution of the quadratic equation Axx + Bx + C = 0
- absolute(q): absolute value of a quantity
- sqrt(q): abbreviation for q^(1/2)
Finally, there are two convenience functions to convert temperatures given on the Celsius
or Fahrenheit scale to a temperture given on the Kelvin scale. For technical reasons,
the argument has to be given as a dimensionless number
- CtoKscale(q): yields the temperature (with units), e.g. CtoKscale(0.01) = 273.16 K
- FtoKscale(q): yields the temperature (with units)
Error messages and warnings
Operations with physical quantities have more limitations than those with pure numbers. For example, you can't add
quantities of different dimensions, and you can't take the logarithm of a quantity unless it is dimensionless, i.e.
lacks units. PQcalc gives specific and detailed error messages and warnings, and shows which part of the calculation resulted in
an error. Once you understand the error, you can press the back button on the browser to get back to the commands that
resulted in the error, and fix it.
v = dist/t_a + t_b |
dist/t_a + t_b
Calculation failed: Units in sum not compatible
problem=distta+tb
=4.52 m0.05 s+52.100 s
=90. ms+52.100 s
|
log_fails = log(dist) |
log(dist)
Calculation failed: Can't take log() of quantity with units
problem=log(dist)
=log(4.52 m)
|
sqrt_ok = sqrt(t_a * t_b) |
sqrtok=√ta⋅tb
=√0.05 s⋅52.100 s
=√2.61 s2
=1.6 s
|
sqrt_warning = sqrt(t_a) |
sqrtwarning=√ta
=√0.05 s
=0.22 s1/2
Warning: Units have non-integer exponents 1/2
|
Viewing results
The output of a calculation shows all the steps. First, the commands input by the user are shown, then the values
are substituted, and then results of the requested operations are shown step by step. Scanning from one line of output
to the next, you can follow in which order the operations were done, what happens to the units along the way, and what
the final result is. The intermediate results are shown with more digits than the significant digits ("guard digits"),
but the final result is shown with the appropriate number of significant digits. To switch the result (and any further
calculations done with that quantity) to a different set of units, you use a separate command, "using", as illustrated
below. To see quantities in units that are not hard-coded into PQcalc, you first have to define the unit (as a quantity)
and then use the command "in" to see a quantity in that non-standard unit.
ΔP = 5.0 atm - 760 mmHg |
ΔP=5.0 atm−760 mmHg
=4.0 atm
|
ΔP using mmHg |
ΔP=4.0 atm
=3.0×103 mmHg
|
ΔP using m kg |
ΔP=3.0×103 mmHg
=4.1×105 kgm s2
|
ΔP using Pa |
ΔP=4.1×105 kgm s2
=4.1×105 Pa
|
psi = 14.69594877551 atm
ΔP in psi |
psi=14.69594877551 atm
ΔP=ΔPpsi psi
=ΔP14.69594877551 atm psi
=4.1×105 Pa14.69594877551 atm psi
=0.272 psi
|
P2 = ΔP * 2 |
P2=ΔP⋅2
=4.1×105 Pa⋅2
=8.1×105 Pa
|
Adapting to the level of the user
There are five so-called switches that change the behavior of PQcalc to adopt to the level of the user. The switches are
__tutor__, __checkunits__, __skip__, __showuncert__, and __fracunits__. They are turned on by assigning "1" to them, and turned off by
assigning "0" to them. The default behaviour is for an intermediate student.
The __tutor__ and __checkunits__ switches
are for the beginning student. __tutor__ mirrors the student input as plain text in the output before showing the
formatted output. This gives the student a direct comparison of plain text input and interpretation, and is good practice
of when parentheses are needed in the linear plain text input. The __checkunits__ switch causes PQcalc to infer the
dimensions of quantities from the first letter of the chosen name. For example, t0 is expected to have dimensions of time,
while T0 is expected to have dimensions of temperature. If the units given to the quantities do not meet these expectations,
a warning message is issued.
The switches __skip__, __showuncert__ and __fracunits__ are for advanced students. Turning on __skip__ suppresses showing the intermediate
steps in the arithmetic. This may be advantageous for multi-step calculations to save screen space and allow a better overview
of the entire argument leading to the desired result. Turning on __showuncert__ switches to a slightly more sophisticated
treatment of error propagation, where the uncertainty in the least significant digit is given explicitly. For example, a result
would be given as a = 4.531(5) instead of a = 4.531. It is possible to enter numbers in this format as well, if uncertainties
are known to this level of detail (e.g. if average value and standard error of the average are known based on multiple
measurements of a quantity). Turning on __fracunits__ suppresses a warning message when units have fractional exponents, e.g.
when calculating a rate constant reaction of a reaction with reaction order one half.
To automatically turn on switches for a group of students, you can set switches in the link to the PQcalc site. The switches
stay turned on for the remainder of the session unless turned of explicitly by setting them to zero, or by pressing the reset button.
For example, running PQcalc through the following link would turn on features for beginning students: ktheis.pythonanywhere.com/custom?switch=tutor*checkunits.
__tutor__ = 1
m[MgSO4] = 34.563 g |
>>> m[MgSO4] = 34.563 g
mMgSO4=34.563 g
|
__checkunits__ = 1
m[NaCl] = 3.4 mol |
mNaCl=3.4 mol
Warning: mNaCl looks like a mass, but units are strange
|
|
|
#normal behaviour, no switches
m_total = m[MgSO4] + m_beaker |
mtotal=mMgSO4+mbeaker
=34.563 g+128.3 g
=162.9 g
|
|
|
__skip__ = 1
m_total = m[MgSO4] + m_beaker |
mtotal=mMgSO4+mbeaker
=162.9 g
|
__showuncert__ = 1
m_total = m[MgSO4] + 256.6(2) g |
mtotal=mMgSO4+256.6(2) g
=34.563(1) g+256.6(2) g
=291.2(2) g
|
Features specifically for teaching
To make sure students don't forget how the to the arithmetic with numbers and units while using PQcalc, there are two
switches that hide either units or numbers in the calculation output. Using these switches, it is easy to make a
worksheet for practicing these mathematical skills, or to project a partial solution in the class room as basis for
discussing these aspects. If an instructor does not wish students to use PQcalc for a given homework assignment, the
use of certain “magic” numbers (such as “19.9351”) in a problem will be detected by PQcalc, and the calculator will not
proceed with the calculation.
Two switches help in formatting output. Turning on the __latex__ switch produces LaTeX output instead of formatted output,
making it easy to copy and paste math and chemistry into a LaTeX document. Turning on the __scrunch__ switch decreases the
line spacing, which sometimes is useful for printing a calculation. To print, use the printing feature of the browser, and
to add a calculation into a slide, take a screen shot.
The __oneline__ switch changes the input box to a single line. Hitting the return key on the keyboard now submits the
command rather than resulting a line break. This feature might be helpful in a class room situation where it is difficult
to switch from keyboard to mouse, and where the instructor wants to give one line of input, submit it, discuss the results
and then type in the next line of input.
t_Trip = 4.3 h
d_Trip = 231. km |
tTrip=4.30 h
dTrip=231. km
|
__hideunits__ = 1
v_Trip = d_Trip / t_Trip |
vTrip=dTriptTrip
=231.kmmolkgmol4.30kmmolkgmol
=53.7kmmolkgmol
|
__hidenumbers__ = 1
v_Trip = d_Trip / t_Trip |
vTrip=dTriptTrip
=231. km4.30 h
=____________ kmh
|
Features of the user interface
Apart from the textbox for typed user input, the user interface includes some selectors and buttons to help using the calculator.
- Shortcut buttons and selectors
The selectors under the input box may be used to save some typing effort, and to have access to some commonly used symbols.
In particular, the quantities selector gives access to all the defined names of quantities. Alternatively, some browsers
allow clicking on the results above the input box to paste quantity names into the input box.
The buttons labeled "+", "-", "*", "/", "()", "=", "[]" provide shortcuts to the respective symbols, and are particularly useful
on mobile devices lacking a full keyboard.
- Help
The "help" button brings up a short text including a link to this manual, followed by a series of examples illustrating
features of PQcalc. To work through an example, click on the link. This will preload commands into the input box. Once you
click "submit", you can see the results of that particular calculation.
- Communicating solutions and results
The "export" button will open a new page summarizing the session in three parts. The first part shows all defined
quantities. The second part shows the commands that have been input during the session. Finally, the third part shows
the result of the calculations. The first and second part of the page are not typeset, but are rather given in plain
text, allowing for uncomplicated copy and paste into emails, learning management systems and other forms of peer-to-peer
and student-teacher channels of communication. This makes it easier to share model solutions or to submit homework.
Limitations
Internally, PQcalc stores numbers in a format that is limited both in terms of the largest number and the precision
of the numbers. The largest number is about 2×10308, and if numbers are larger than that, it will result in
an error. The smallest positive number is about 2×10−16, and if you try to make an even smaller number,
it will turn into zero. Because of the way numbers are stored, they have a maximum of 16 significant figures, so if
more digits are displayed, they carry no information. Most calculations are not affected by these numerical limitations,
although very small or very large numbers might be encountered when doing subatomic or astronomical calculations in
SI units, the units PQcalc uses internally.
PQcalc works exclusively with scalar real quantities. Thus, it does not support imaginary and complex numbers. It also
does not support vectors, matrices or tensors. While it would be trivial to expand the program to deal with these, it
would complicate the user interface substantially. It also does not include statistics functions or ways of dealing
with large sets of measurements simultaneously. When students have reached a level where this degree of complexity
is required to solve problems, it might make sense to switch to a full-fledged math or statistics software package
such as matlab, mathematica, R or sage.
Significant figures
Internally, PQcalc assigns every quantity an uncertainty. Quantities defined by their value (e.g. m = 2.4 g)
are assigned an uncertainty based on the last digit given, i.e. 0.1 g in the example. For values that lack decimal
points (such as n = 1 mol), the uncertainty is set to zero. It is also possible to enter uncertainty using a
parentheses format (e.g. m = 2.423(7) g), which in the example would be interpreted as an uncertainty of 0.007 g.
For derived quantities, uncertainties are propagated using a model that assumes gaussian distributions of errors.
If there are two quantities q1 and q2 with values v1 and v2 and uncertainties d1 and d2, the uncertainty d3 of the
quantity q3 given by the possible binary operations q3 = (q1 op q2) is estimated as:
- Addition/Subtraction: d3=√d12+d22
- Multiplication/Division: d3=|v3|⋅√(d1v1)2+(d2v2)2
- Raising q1 to the power of q2: d3=|v3|⋅(|d1v1⋅v2|+d2⋅ln(|v1|))
For unary functions q2 = f(q1), the uncertainty d2 of q2 is calculated as:
- exp(q1): d2=d1⋅|v2|
- ln(q1): d2=d1|v1|
- log(q1): d2=d1|v1|
To let the user explore how error propagation is estimated in PQcalc and how uncertainties are shown as significant
figures (normal behaviour) or in parentheses (i.e. with the __showuncert__ switch on), PQcalc has two functions that show
the value of a quantity with more digits (moredigits(q)) and the uncertainty d associated with a quantity
(uncertainty(q)). In calculations, intermediate values are shown with an additional 'guard digit' to be able
to follow and recapitulate intermediate steps without loosing information.
Installing a server
The source files necessary for installing PQcalc are available on GitHub under ktheis/pqcalc:
- quantities.py
- calculate.py
- server.py
- form.py
To run a local server, you need to have
Python 2.7 installed, together with the package web.py. After placing all python source files into a common
directory, you run the server.py script and keep it running as long as you want to serve the PQcalc application. In
the browser, you go to 'localhost:8080' to run the application. PQcalc will access one external site,
http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML, containing the code to markup
mathematical and chemical formulas. It is possible to install a local copy of this as well if PQcalc should run
on a machine not connected to the internet (you have to change the hyperlink in the form.py file).
To run a server on the internet, you either can use a provider (such as pythonanywhere.com) or run your own host.
Again, you will have to have Python 2.7 and web.py installed (with some changes in the server.py file, you could
also use a web framework other than web.py). The details of how to start and monitor the web site will depend on the
specific circumstances, and it would probably be best to work together with someone who has experience in running
a web application.