Computer Science 121

advertisement
Computer Science 121
Scientific Computing
Winter 2014
Chapter 1: What is Computation?
1.1 Computation as Transformation
• A computation is a transformation from on or
more inputs to an output.
– Input = time;
Output = reactant (grams)
1.1 Computation as Transformation
•
Input = age (years);
Output = recall (items)
1.1 Computation as Transformation
Input = x;
Output =√x
http://www.macalester.edu/~kaplan/scientificprogramming/figures.ppt
1.1 Computation as Transformation
Input = financial info;
Output = tax owed / refunded
http://www.macalester.edu/~kaplan/scientificprogramming/figures.ppt
1.1 Computation as Transformation
Input = image file;
Output = “This is the Mona Lisa!”
http://www.macalester.edu/~kaplan/scientificprogramming/figures.ppt
1.1 Computation as Transformation
Input = .wav file;
Output = “This is the vowel /i/”
1.2 Computation as Reaction to
Events
• Inputs often come from measurements
• Measurements are typically performed using
transducers (convert input signal into numbers)
– microphone
– speedometer
– thermometer
– seismograph
– radar
• (What sort of data doesn't require transducers?)
1.2 Computation as Reaction to
Events
– Sundial (and compass): Analog transducers
http://research.utk.edu/ora/explore/sundial.jpg
1.2 Computation as Reaction to
Events
– Because measurements (signals) typically change over
time, we get the concept of state : the set of values
describing a system at a given time
– E.g.
• Health: <blood pressure, temperature, heart rate>
• Airplane: <heading, altitude, fuel>
• Subatomic particle: <position, momentum>
– So...
• A computation is a transition from an old state
to a new state.
1.2 Computation as Reaction to
Events
– A computation is a transition from an old state to a
new state
– What does this mean?
– If we can build a computational model of how a
system changes state (reacts to events), we can make
predictions about how the system will behave
• Health: influence of drugs
• Airplane: response to turbulence
• Particle: Heisenberg's Uncertainty Principle (!!!)
1.3 Algorithms
Muhammad ibn Mūsā
al-Khwārizmī
(c. 780 – 850)
Al-Kitāb al-mukhtaṣar fī hisāb
al-jabr wa-l-muqābala
1.3 Algorithms
– “Don't Reinvent the Wheel” Principle: We should
build new computations out of ones that already exist
– Principle applies across the board in computer science:
• How computer runs programs:
Matlab Programs
Matlab Interpreter
Operating System
Hardware
1.3 Algorithms
– Principle applies across the board
• How programs are written:
my_program.m
read_data.m save_data.m plot_data.m
1.3 Algorithms
– Build new computations out of ones that already exist
– An algorithm is a set of directions for carrying out a
computation in terms of other, simpler computations.
– Difficulty in writing algorithms comes from
differences between the ways that people do things and
the way that computers do them
• People: Slow, parallel, good generalization
1.3 Algorithms
– People: Slow, parallel, good generalization
“Cat!”
“Cat!”
“Cat!”
1.3 Algorithms
– Computers: Fast, serial, poor generalization
6068
x 4859
29484412
Expressing Algorithms
• Example: Compare two Arabic numerals (e.g., 23
and 97) to determine which is larger
– Count the digits in each numeral. If one has
more digits than another, the one with more is
the larger.
– Otherwise, compare the leftmost digits in the
two numerals. If one digit is larger than the
other, the numeral with the larger leftmost digit
is the larger numeral.
– Otherwise, chop off the first numeral and go to
step 2.
Q.: What haven't we considered?
Expressing Algorithms
• Example: Find smallest number in list
1. Assume smallest is first item in list.
2. If there are no more items in list, we're done.
Report smallest number.
3. Otherwise, look at next item. If it's smaller than
current assumed “smallest” value, make current
answer equal to value of this item.
4. Go to step 2.
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 5
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 2
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 2
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 1
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 1
9
1
3
Expressing Algorithms
• Example: Find smallest
LIST: 5 2
SMALLEST: 1
9
1
3
Expressing Algorithms
• Example: Sort a list of numbers
1. Create a new, empty list
2. Find the smallest item in the old list.
3. Remove this item from the old list and add it to
the beginning of the new list.
4. If the old list is empty, we're done. Output the
new list.
5. Otherwise, go to step 2.
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
NEW:
5
2
9
1
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
NEW:
5
2
9
1
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
NEW:
1
2
9
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
NEW:
1
2
9
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
9
NEW:
1
2
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
9
NEW:
1
2
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
9
NEW:
1
2
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
5
9
NEW:
1
2
3
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
9
NEW:
1
2
3
5
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
9
NEW:
1
2
3
5
Expressing Algorithms
• Example: Sort a list of numbers
OLD:
NEW:
1
2
3 5
9
Important Points
• We re-used our smallest-number algorithm in our sorting
algorithm: instead of re-writing it, we referred to it!
• We needed a strange notion of “smallest” - smallest item in a oneitem list is the item itself.
• Contrast this with ordinary language terminology
– No “smallest” (or largest) item in a one-item list.
– We (are supposed to) say “smaller” (not “smallest”) if there
are only two items
– These sorts of differences contribute to our difficulty in
learning to program.
– But as algorithms get more complicated, we will find it more
difficult to use ordinary language (“the current smallest”) to
express them – need variables
Download