Programming Languages & Tools



Similar documents
CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

McGraw-Hill The McGraw-Hill Companies, Inc.,

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

What is a programming language?

CS 40 Computing for the Web

Fall 2012 Q530. Programming for Cognitive Science

Chapter 13: Program Development and Programming Languages

Computing, technology & Data Analysis in the Graduate Curriculum. Duncan Temple Lang UC Davis Dept. of Statistics

Numerical Analysis. Professor Donna Calhoun. Fall 2013 Math 465/565. Office : MG241A Office Hours : Wednesday 10:00-12:00 and 1:00-3:00

CS 51 Intro to CS. Art Lee. September 2, 2014

Computational Mathematics with Python

Chapter One Introduction to Programming

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Chapter 12 Programming Concepts and Languages

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

(!' ) "' # "*# "!(!' +,

PGR Computing Programming Skills

Fundamentals of Programming and Software Development Lesson Objectives

Computational Mathematics with Python

Scientific Programming, Analysis, and Visualization with Python. Mteor 227 Fall 2015

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

HPC Wales Skills Academy Course Catalogue 2015

Introduction to Python

Computational Mathematics with Python

Solution of Linear Systems

Operation Count; Numerical Linear Algebra

Java in Education. Choosing appropriate tool for creating multimedia is the first step in multimedia design

Modeling with Python

Maple Quick Start. Introduction. Talking to Maple. Using [ENTER] 3 (2.1)

Introduction to Scientific Computing

Moving from CS 61A Scheme to CS 61B Java

Why (and Why Not) to Use Fortran

Lines & Planes. Packages: linalg, plots. Commands: evalm, spacecurve, plot3d, display, solve, implicitplot, dotprod, seq, implicitplot3d.

Fast Arithmetic Coding (FastAC) Implementations

CSCI 3136 Principles of Programming Languages

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

1/20/2016 INTRODUCTION

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

2) Write in detail the issues in the design of code generator.

Chapter 6: Programming Languages

COS 333: Advanced Programming Techniques

3 SOFTWARE AND PROGRAMMING LANGUAGES

MBA Jump Start Program

Software: Systems and. Application Software. Software and Hardware. Types of Software. Software can represent 75% or more of the total cost of an IS.

Architectures for Big Data Analytics A database perspective

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

Chapter 13: Program Development and Programming Languages

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

Evolution of the Major Programming Languages

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

CSC Software II: Principles of Programming Languages

A Comparison of C, MATLAB, and Python as Teaching Languages in Engineering

Computer Layers. Hardware BOOT. Operating System. Applications

13 File Output and Input

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

High level code and machine code

Practical Programming, 2nd Edition

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

SCIENTIFIC COMPUTING AND PROGRAMMING IN THE CLOUD USING OPEN SOURCE PLATFORMS: AN ILLUSTRATION USING WEIGHTED VOTING SYSTEMS

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Scientific Programming in Python

The programming language C. sws1 1

WHITE PAPER. Peter Drucker. intentsoft.com 2014, Intentional Software Corporation

The Julia Language Seminar Talk. Francisco Vidal Meca

Levels of Programming Languages. Gerald Penn CSC 324

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

CSE 307: Principles of Programming Languages

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

The C Programming Language course syllabus associate level

ANALYTICS CENTER LEARNING PROGRAM

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Sources: On the Web: Slides will be available on:

Outline. High Performance Computing (HPC) Big Data meets HPC. Case Studies: Some facts about Big Data Technologies HPC and Big Data converging

Python Programming: An Introduction to Computer Science

Unlocking the True Value of Hadoop with Open Data Science

How to Design and Create Your Own Custom Ext Rep

Introduction. Chapter 1

Survey, Statistics and Psychometrics Core Research Facility University of Nebraska-Lincoln. Log-Rank Test for More Than Two Groups

Python for Scientific Computing.

1 Topic. 2 Scilab. 2.1 What is Scilab?

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Linear Algebra and TI 89

MATLAB Basics MATLAB numbers and numeric formats

El Dorado Union High School District Educational Services

Today's Topics. COMP 388/441: Human-Computer Interaction. simple 2D plotting. 1D techniques. Ancient plotting techniques. Data Visualization:

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

Introduction to Python

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

FDVis: the Interactive Visualization and Steering Environment for the Computational Processes Using the Finite-Difference Method

Transcription:

4 Programming Languages & Tools Almost any programming language one is familiar with can be used for computational work (despite the fact that some people believe strongly that their own favorite programming language is superior to all other languages). Figure 4-1 categorizes programming language from the perspective of scientific computing. 4.1 High-level programming languages High-level programming languages received their name because they spare the programmer many details of what is happening in the CPU, which is described by low-level machine code. From a modern point of view, high level languages require relatively detailed instructions, and are flanked by very high level languages. C and Fortran, for example, are well suited for intensive scientific computing. Both languages are fast in execution, quick to program, and widely known. There exist large program repositories and excellent compilers for them. C is the common tongue of programmers and computer scientists. Modern C includes intrinsic complex arithmetic, which was absent from early C standards. C++ is a much larger and more complex language than C. And C is a subset of C++. C++ becomes advantageous when code gets large and needs to be maintained. Fortran is a programming language tailored to the needs of scientists and engineers and as such it continues to be particularly well suited for this purpose. Modern Fortran (Fortran 90 and later versions) greatly 18

Chapter 4 19 Mid- and High-level Programming Languages: C Fortran (77 & modern) C++ Java Scripting Languages & Stream Editors: awk perl sed BigData: SQL Hadoop General-purpose Mathematical Software Packages: IDL (and GDL) Matlab (and Octave) Mathematica Python Data Visualization & Graphing Software: Grace Gnuplot Origin plotutils matplotlib, Special purpose software: Statistics: R, SAS, SPSS, Astrophysics: IRAF, ISIS, Mercury, LabView Figure 4-1: Programming languages and tools organized from the perspective of scientific computing extends the capabilities of earlier Fortran standards. A major advantage of Fortran are its parallel computing abilities. Fortran 77, compared to Fortran 90, lacks dynamic memory allocation, that is, the size of an array cannot be changed while the program is executing. Because of its simplicity and age, compiler optimization for Fortran 77 is probably the

20 Lessons in Computational Physics best available for any language. Python is versatile and enables programmers to write code in less time, which makes it highly suited for scientific programming, but Python code tends to be slow. The appropriate saying is that Python for comfort, Fortran for speed. If you know one high-level language, you can quickly understand another and switch between the two. Table 4-I shows a program in C and in Fortran that demonstrates similarities between the two languages. /* C program example */ #include <math.h> #include <stdio.h> void main() { int i; const int N=64; float b,a[n]; b=-2.; for(i=0;i<n;i++) { a[i]=sin(i/2.); if (a[i]>b) b=a[i]; } b=pow(b,5.); b=b/n; printf("%f\n",b); }! Fortran program example program demo implicit none integer i integer,parameter :: N=64 real b,a(n) b=-2. do i=1,n a(i)=sin((i-1)/2.) if (a(i)>b) b=a(i) enddo b=b**5; b=b/n print *,b end Table 4-I: Program examples that demonstrate similarities between two languages. The following features are not analogous: C is case-sensitive, Fortran is not. Array indices begin by default with 0 for C and with 1 for Fortran. Fortran output lines automatically end with a line break, not so in C. Recommended Reading: The best reference book on C is Kernighan & Ritchie, The C Programming Language, although as an introductory book it is challenging. A concise but complete coverage of Fortran is

Chapter 4 21 given by Metcalf, Reid & Cohen, Modern Fortran Explained. There is also abundant instructional and reference material online. 4.2 Some relevant language features Program code can be made executable by interpreters, compilers, or a combination of both. Interpreters read and immediately execute the source program line by line. Compilers process the entire program before it is executed, which permits better checking and speed optimization. Language implementations that can be compiled hence execute much faster than interpreted ones. Just-in-time compilers perform compilation while the program is executing (e.g. Java). (A fourth but rare possibility are source-to-source translators that convert the language statements into those of another.) Fortran and Matlab are examples of languages that allow vectorized or index-free notation. An operation is applied to every element of an entire array, for example A(:)=B(:)+1. This not only saves the work of writing a loop command, but also makes it obvious (to the compiler) that the task can be parallelized. With explicit loops the compiler first needs to determine whether or not the commands within the loop are independent from one another. Some languages, such as Fortran and Python, allow unformatted output that does not truncate the internal binary representation. Unformatted output exactly preserves the internal accuracy of numbers. (Compare C s printf("%f\n",2.1); with Fortran s print *,2.1). Common implementations of Java and Python are built on an intermediate layer, a Virtual Machine, which enhances portability but inhibits computational performance. Many general-purpose text editors, such as Emacs, vi, and BBEdit, support programming with auto-indent, syntax coloring, highlighting of matching pairs of braces, and other helpful features.

22 Lessons in Computational Physics 4.3 General-purpose mathematical software There are ready-made software packages for numerical calculations. Many tasks that would otherwise require lengthy programs can be accomplished with a few keystrokes. For instance, it only takes one command to find a root, say FindRoot[sin(3 x)==x,{x,1}] (in Mathematica notation). The command searches for a solution to the equation beginning with x = 1. Inverting a matrix reduces to Inverse[A] (in Mathematica) or 1/A (in Matlab). They have many intrinsic functions. Such software tools have become so convenient and powerful that they are the preferred choice for many computational problems. Table 4-II shows the example program above in two popular applications. These programs are considerably shorter than those in Table 4-I and do not require variables to be declared. % Matlab program example N=64; i=[0:n-1]; a=sin(i/2); b=max(a); b^5/n ; IDL program example N=64 a=fltarr(n) FOR i=0,n-1 DO a(i)=sin(i/2) b=max(a) PRINT, b^5/n Table 4-II: Matlab and IDL program examples; compare to Table 4-I. In Matlab, a semicolon at the end of the line surpresses output. It also has index-free notation. Programs can be written for such software packages in their own application-specific language. Often these do not achieve the speed possible with languages like Fortran or C. One reason for that is the trade-off between universality and efficiency a general method is not going to be the fastest. Further, we typically do not have access to the source codes to adjust them. Another reason is that, although individual commands may be highly efficient computationally, a succession of commands is interpreted and hence slow. Major general-purpose mathematical software packages that are

Chapter 4 23 currently popular: Mathematica began as a symbolic computation package, which is still is its comparative strength. Matlab is particularly strong in linear algebra tasks; its name is an abbreviation of Matrix Laboratory. Octave is open-source software that mimics Matlab. IDL (Interactive Data Language) began as a visualization and data analysis package. Its syntax defines the Gnu Data Language (GDL). All of these software packages offer a wide range of numerical and graphical capabilities. As an additional example, here is a Python implementation of the program example shown in Tables 4-I and 4-II: # Python program example N=64 import math a=[] for i in range (0,N-1): a.append(sin(i/2.)) b=max(a) print b**5/n (It is amusing that in the five language examples above, each uses a different symbol to indicate a comment line.) 4.4 Data visualization & scientific graphing Graphics is an indispensable tool for data analysis, program testing, and scientific inquiry. We only want to avoid spending too much time on learning and coping with graphics software. Often, data analysis is exploratory. It is thus desirable to be able to produce a graph quickly and with ease. Simple graphics software can go a long way. Gnuplot is a simple and free graphics plotting program, http:// www.gnuplot.info. It is quick to use and learn. A single command suffices to load and plot data. For example, plot 'stuff.dat' u 1:3 w lp plots column 3 versus column 1 and uses lines and points in the graph.

24 Lessons in Computational Physics Another, similar tool is Grace, also freely available. Origin is one of many widely used proprietary software packages. All general-purpose math packages listed above have powerful graphing and visualization capabilities. Choosing a programming language: Whether it is better to use a ready-made mathematical software package or write a program in a lower level language like C or Fortran depends on the task to be solved. Each has its domain of applicability. A single language or tool will be able to deal with a wide range of tasks, if needed, but will be inefficient or cumbersome for some of them. To be able to efficiently deal with a wide range of computational problems, it is advantageous to know several languages or tools from different parts of the spectrum (bold boxes in Fig. 4-1): One high-level programming language for time-intensive number-crunching tasks, one general-purpose software for every-day calculations and data visualization, and a few small tools for data analysis. This enables a scientist to choose a tool appropriate for the given task. Knowing multiple languages from the same category provides only a marginal advantage and increases the chance for confusing syntax.