Skip to main content
Top
Published in:
Cover of the book

2012 | OriginalPaper | Chapter

1. Computing with Formulas

Author : Hans Petter Langtangen

Published in: A Primer on Scientific Programming with Python

Publisher: Springer Berlin Heidelberg

Activate our intelligent search to find suitable subject content or patents.

search-config
loading …

Abstract

Our first examples on computer programming involve programs that evaluate mathematical formulas. You will learn how to write and run a Python program, how to work with variables, how to compute with mathematical functions such as e x and sinx, and how to use Python for interactive calculations.

Dont have a licence yet? Then find out more about our products and how to get one now:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Footnotes
1
Another frequent word for folder is directory.
 
2
This formula neglects air resistance, which is usually small unless v 0 is large – see Exercise 1.11.
 
3
Other examples are OpenOffice, TextEdit, iWork Pages, and BBEdit. Chapter 6.​1.​3 gives some insight into why such programs are not suitable for writing your own Python programs.
 
4
On Mac, you may want to download a more “Mac-like” editor such as the Really Simple Text program.
 
5
The ampersand after idle is Linux specific. On Windows you have to write start idle instead. The ampersand postfix or the start prefix makes it possible to continue with other commands in the terminal window while the program, here Idle, is running. This is important for program testing where we often do a lot of edit-and-run cycles, which means that we frequently switch between the editor and the terminal window.
 
6
Some editors, like Emacs, have many features that make it easier to write Python programs, but these features will not be automatically available unless the program file has a .py extension.
 
7
“Programming demands significantly higher standard of accuracy. Things don’t simply have to make sense to another human being, they must make sense to a computer.” – Donald Knuth [4, p. 18], computer scientist, 1938–.
 
8
There are other ways of running Python programs, as explained in Appendix H.1.
 
9
In this book we shall adopt the rule that variable names have lower case letters where words are separated by an underscore. The first two declared variables have this form.
 
10
This formatting was originally introduced by a function printf in the C programming language.
 
11
For a complete specification of the possible printf-style format strings, follow the link from the item “printf-style formatting” in the index of the Python Library Reference.
 
12
Note that text files normally have the extension .txt, while program files have an extension related to the programming language, e.g., .py for Python programs. The content of a .py file is, nevertheless, plain text as in a .txt file.
 
13
In the very early days of computing, computers were built of a large number of tubes, which glowed and gave off heat. The heat attracted bugs, which caused short circuits. “Debugging” meant shutting down the computer and cleaning out the dead bugs.
 
14
The R (or S or S-PLUS) programming languages for statistical computing actually use an arrow, while other languages such as Algol, Simula, and Pascal use := to explicitly state that we are not dealing with a mathematical equality.
 
15
More precisely, blank is the character produced by the space bar on the keyboard, while whitespace denotes any character(s) that, if printed, do not print ink on the paper: a blank, a tabulator character (produced by backslash followed by t), or a newline character (produced by backslash followed by n). The newline character is explained on page 14.
 
16
The parentheses around 9/5 are not strictly needed, i.e., (9/5)*C is computationally identical to 9/5*C, but parentheses remove any doubt that 9/5*C could mean 9/(5*C). Chapter 1.3.4 has more information on this topic.
 
17
The v1 part of the name stands for “version 1”. Throughout this book, we will often develop several trial versions of a program, but remove the version number in the final version of the program.
 
18
Some mathematical algorithms do make use of integer division, but in those cases you should use a double forward slash, //, as division operator, because this is Python’s way of explicitly indicating integer division.
 
19
Nevertheless, of convenience we often use the from module import * syntax in this book.
 
20
Instead, you can search for “floating point number” on wikipedia.​org.
 
21
Exercise 2.23 on page 84 estimates the size of the errors.
 
22
See the last paragraph of Chapter 3.​1.​9 for an example.
 
23
This key works only if Python was compiled with the Readline library. In case the key does not work, try the editing capabilities in another Python shell, for example, IPython (see Chapter 1.5.3).
 
24
The functions also come into play by a from scipy import * statement or from scitools.std import *. The latter is used as a standard import later in the book.
 
25
But objects can exist without being bound to a name: print ’Hello!’ first makes a string object of the text in quotes and then the contents of this string object, without a name, is printed.
 
26
This formula neglects air resistance. Exercise 1.11 explores how important air resistance is. For a soft kick (v 0=10 km/h) of a football, the gravity force is about 120 times larger than the air resistance. For a hard kick, air resistance may be as important as gravity.
 
27
Recall from Chapter 1.5.3 that we just write the program name. A real execution demands prefixing the program name by python in a DOS/Unix terminal window, or by run if you run the program from an interactive IPython session. We refer to Appendix H.1 for more complete information on running Python programs in different ways.
 
28
Alternatively, the output lines can be inserted as comments, but using a multi-line string requires less typing. (Technically, a string object is created, but not assigned to any name or used for anything in the program – but for a human the text in the string contains useful information.)
 
29
The redirection to files does not work if the program is run inside IPython. In a DOS terminal window you may also choose to redirect output to a file, because cut and paste between the DOS window and the program window does not work by default unless you right-click the top bar, choose Properties and tick off Quick Edit Mode.
 
30
The function is named after Carl Friedrich Gauss, 1777–1855, who was a German mathematician and scientist, now considered as one of the greatest scientists of all time. He contributed to many fields, including number theory, statistics, mathematical analysis, differential geometry, geodesy, electrostatics, astronomy, and optics. Gauss introduced the function (1.6) when he analyzed probabilities related to astronomical data.
 
Literature
[4]
go back to reference D. E. Knuth. Theory and practice. EATCS Bull., 27:14–21, 1985. D. E. Knuth. Theory and practice. EATCS Bull., 27:14–21, 1985.
[6]
go back to reference L. S. Lerner. Physics for Scientists and Engineers. Jones and Barlett, Sudbury, 1996. L. S. Lerner. Physics for Scientists and Engineers. Jones and Barlett, Sudbury, 1996.
Metadata
Title
Computing with Formulas
Author
Hans Petter Langtangen
Copyright Year
2012
Publisher
Springer Berlin Heidelberg
DOI
https://doi.org/10.1007/978-3-642-30293-0_1

Premium Partner