Transcript Lec9.ppt

Experiments in Musical Intelligence
Nelson Series Talk
Wed, 10/27
7:00 pm
HMC’s Galileo Auditorium
Some of Cope’s generated MP3s:
arts.ucsc.edu/faculty/cope/mp3page.htm
David Cope, UC Santa Cruz
I began Experiments in Musical Intelligence in 1981 as the
result of a composer's block. My initial idea involved creating
a computer program which would have a sense of my overall
musical style and the ability to track the ideas of a current
work such that at any given point I could request a next note,
next measure, next ten measures, and so on. My hope was that
this new music would not just be interesting but relevant to my
style and to my current work. Having very little information
about my style, however, I began creating computer programs
which composed complete works in the styles of various
classical composers, about which I felt I knew something more
concrete.
Since the early days of Experiments in Musical Intelligence,
many audiences have heard its output in the styles of classical
composers. The works have delighted, angered, provoked, and
terrified those who have heard them. I do not believe that the
composers and audiences of the future will have the same
reactions. Ultimately, the computer is just a tool with which
we extend our minds. The music our algorithms compose are
just as much ours as the music created by the greatest of our
personal human inspirations.
Week 9 in CS 5
Lab: M-Z
A dizzying array of possibilities…
• HW 9 (2 probs)
due Sunday, 10/31 at midnight
due Monday, 11/1 at midnight
M/T
W/Th
Reading: Week 9’s online notes
Recitation for HW9 -- Friday 10/29
• This week’s credits:
John Conway
Still seeking a costume?
Carl Gauss
“Pass By Value”
public static void main(String[] args)
{
H.pl(“Welcome to Conformity, Inc.”);
int fav = 7;
// The user’s favorite #
int fav
conform(fav);
H.pl(fav + “ is my favorite, too!”);
}
public static void conform(int fav)
{
fav = 42;
return;
}
7
int fav
“Pass By Value”
public static void main(String[] args)
{
H.pl(“Welcome to Conformity, Inc.”);
int fav = 7;
7
// The user’s favorite #
int fav
conform(fav);
H.pl(fav + “ is my favorite, too!”);
PASS
BY
VALUE
}
public static void conform(int fav)
{
fav = 42;
return;
}
7 42
int fav
“Pass by value” means that data is copied when sent to a method
Passing Arrays by Value
public static void main(String[] args)
{
H.pl(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
int[] fav
fav[0]
fav[1]
H.pl(fav[0] + “ and ” + fav[1] + “! Me too!”);
}
public static void conform(int[] fav)
{
fav[0] = 42; fav[1] = 42;
return;
int[]
}
fav
Passing Arrays by Value
public static void main(String[] args)
{
H.pl(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
int[] fav
fav[0]
fav[1]
H.pl(fav[0] + “ and ” + fav[1] + “! Me too!”);
}
public static void conform(int[] fav)
{
fav[0] = 42; fav[1] = 42;
return;
int[]
}
can change data
elsewhere!
fav
Views of the world
Views of the world
Engineers think their equations are an approximation to reality.
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Mathematicians don't care.
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Mathematicians don't care.
Creating structure from a
few simple facts...
?
Axioms
Definitions
20˚
Proof
60˚
10˚
70˚
The John Conway Challenge….
?
20˚
10˚
60˚
70˚
(without using trig)
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Mathematicians don't care.
Creating structure from a
few simple facts...
Axioms
Definitions
Creating structure from a
few simple actions ...
for
arrays
variables
while
arithmetic operations
if/else
Proof
Algorithm
Arrays’ flexibility
Arrays can be of ANY type
double[] A;
A = new double[3];
they don’t have to
be horizontal lists!
42.0
double[]
A
double
75.0
double
70.0
double
Arrays’ flexibility
Arrays can be of ANY type
double[] A;
A = new double[3];
42.0
double[]
A
double
42.0
they don’t have to
be horizontal lists!
double[]
A
double
75.0
double
70.0
double
75.0
double
70.0
double
Arrays’ flexibility
Arrays can be of ANY type
double[] A;
A = new double[3];
42.0
double[]
A
int[] A;
A = new int[3];
42
int[]
A
String[] A;
A = new String[3];
double
int
“go”
String[]
A
String
75.0
double
7
int
70.0
double
-11
int
“red”
String
String
2d arrays
Arrays can be of ANY type -- so what type of array is A ?
double[][] A;
double[][]
A
2d arrays
Arrays can be of ANY type -- even other arrays!
double[][] A;
A is an array of
double[][]
A
double arrays!
2d arrays
Arrays can be of ANY type -- even other arrays!
double[][] A;
A = new double[3][];
double[][]
A
double[]
A[0]
double[]
A[1]
double[]
A[2]
Jagged arrays
Rows of 2d arrays need not be the same length!
double[][] A;
A = new double[3][];
double[][]
A
double[]
A[0]
double
double
double[]
A[1]
double
double
double[]
A[2]
double
double
double
double
double
double
A[0] = new double[4];
A[1] = new double[2];
A[2] = new double[5];
double
We will not use jagged arrays
Rows of 2d arrays need not be the same length!
double[][] A;
A = new double[3][];
double[][]
A
double[]
A[0]
double
double
double[]
A[1]
double
double
double[]
A[2]
double
double
double
double
double
double
A[0] = new double[4];
A[1] = new double[2];
A[2] = new double[5];
double
Rectangular arrays
But there’s a shortcut to creating them if they are...
double[][] A; rows cols
A = new double[3][4];
double[][]
A
double[]
A[0]
double
double
double
double
double[]
A[1]
double
double
double
double
double[]
A[2]
double
double
double
double
2d arrays
Getting at individual elements:
rows
cols
double[][] A = new double[3][4];
double[][]
A
double[]
A[0]
double
A[0][0]
double
double
double
double[]
A[1]
double
double
double
double
double[]
A[2]
double
double
double
double
A[2][3]
A[1][2] = 10.0
2d arrays: Input
public static void main(String[] args)
{
double[][] A = new double[3][4];
these will be
variables
H.pl(“Input your values:”)
for (int r=0 ; r<3 ; ++r)
{
for (int c=0 ; c<4 ; ++c)
{
A[r][c] = H.nd();
}
}
no i or j ?
2d arrays: Input Method
public static void enterValues(double[][] A)
{
how do we find the
number of rows and the
H.pl(“Input your values:”);
number of columns?
for (int r=0 ; r <
; ++r)
number of rows
{
for (int c=0 ; c <
; ++c)
number of columns
{
A[r][c] = H.nd();
}
}
}
2d arrays: Output
public static void print(double[][] A)
{
for (int r = 0; r < A.length; ++r)
number of rows
{
for (int c = 0; c < A[r].length; ++c)
number of columns in row r
{
H.p(
}
}
}
I/O formatting reference:
http://www.cs.hmc.edu/courses/2004/fall/cs5/HMCSupport.html
Problem 1
An array of array handlers...
Initial Setup
Get the number of rows and columns
Create an array of the appropriate number of elements (doubles).
Get initial values from the user into the array.
Menu
(0) Change the values in the array
(1) Print the array
(2) Multiply an array row
(3) Add one row to another
(4) Add a multiple of one row to another
(5) Solve!
printMenu
(9) Quit
addMxRowaIntoRowb
Which choice would you like?
solve
Methods
enterValues
print
multRow
addRowaIntoRowb
A
“Quiz”
Starting with the 2d array A
shown here, what are the values
in A after running this code?
row 0
row 1
row 2
public static void mysteryMethod(int[][] A)
{
for (int r = 0 ; r < A.length ; ++r)
{
for (int c = 0 ; c < A[r].length ; ++c)
{
if (r == c)
{
A[r][c] = 42;
}
else
{
A[r][c] = A[r][c] + 1;
}
}
}
} // end of mystery method
Before
1
5
9
2
6
10
col 0
col 1
A
3
7
11
col 2
4
8
12
col 3
After
What are the resulting values in A?
“Quiz”
Write a method that
adds two times the
values in row #1 into
the values in row #2.
The values in row #1
should not change!
(The values in row #0
also don’t change!)
example
row 0
row 1
two of row 1
are to be
added to row 2
row 2
row 0
row 1
row 2
A
before
0.00
3.00
10.00
1.00
0.50
20.00
0.00
0.50
30.00
1.00
3.00
40.00
0.00
3.00
16.00
1.00
0.50
21.00
0.00
0.50
31.00
1.00
3.00
46.00
example
A
after
public static void addTwoOfRow1IntoRow2(double[][] A)
{
}
Adding one row to another...
A
A
Before
2.00
-3.00
1.00
3.00
-1.00
-9.00
-1.00
2.00
4.00
-8.00
42.00
56.00
After
2.00
-3.00
3.00
3.00
-1.00
-6.00
-1.00
2.00
3.00
-8.00
42.00
48.00
public static void addRowaIntoRowb(double[][] A, int ra, int rb)
{
}
Adding one row to another...
A
A
Before
2.00
-3.00
1.00
3.00
-1.00
-9.00
-1.00
2.00
4.00
-8.00
42.00
56.00
After
2.00
-3.00
3.00
3.00
-1.00
-6.00
-1.00
2.00
3.00
0
-8.00
42.00
48.00
2
public static void addRowaIntoRowb(double[][] A, int ra, int rb)
{
}
Problem 1
An array of array handlers...
Initial Setup
Get the number of rows and columns
Create an array of the appropriate number of elements (doubles).
Get initial values from the user into the array.
Menu
(0) Change the values in the array
(1) Print the array
(2) Multiply an array row
(3) Add one row to another
(4) Add a multiple of one row to another
(5) Solve!
printMenu
(9) Quit
addMxRowaIntoRowb
Which choice would you like?
solve
Methods
enterValues
print
multRow
addRowaIntoRowb
Gaussian Elimination
Carl Gauss is
so money!
2p
-3p
1p
+
+
+
Goal: Find
3n
-1n
-9n
p,n,q
+
+
+
-1q
2q
4q
=
=
=
-8.00
42.00
56.00
Gaussian Elimination
2p
-3p
1p
+
+
+
3n
-1n
-9n
+
+
+
+
+
+
0n
1n
0n
=
=
=
-8.00
42.00
56.00
• get 1s along the diagonal
• get 0s elsewhere on the left
Goal: Find p,n,q
1p
0p
0p
-1q
2q
4q
+
+
+
0q
0q
1q
=
=
=
1.00
5.00
25.00
Gaussian Elimination
2p
-3p
1p
+
+
+
3n
-1n
-9n
+
+
+
+
+
+
0n
1n
0n
=
=
=
-8.00
42.00
56.00
• get 1s along the diagonal
• get 0s elsewhere on the left
Goal: Find p,n,q
1p
0p
0p
-1q
2q
4q
Carl Gauss
is money.
+
+
+
0q
0q
1q
=
=
=
1.00
5.00
25.00
Using only row operations (our methods) !
Just the array is necessary !
We can get rid of the variables...
where to start?
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
multiply Row 0 by 0.5
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
a hint as to the direction
to head next… !
multiply Row 0 by 0.5
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
multiply Row 0 by 0.5
1.00
0.00
0.00
1.50
3.50
-10.50
-0.50
0.50
4.50
-4.00
30.00
44.00
add 3 times Row 0 to Row 1
add -1 times Row 0 to Row 2
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
multiply Row 0 by 0.5
1.00
0.00
0.00
1.50
3.50
-10.50
-0.50
0.50
4.50
-4.00
30.00
44.00
add 3 times Row 0 to Row 1
add -1 times Row 0 to Row 2
1.00
0.00
0.00
0.00
1.00
0.00
-0.71
0.14
6.00
-16.85
30.00
150.00
add a multiple of Row 1 to Row 0
multiply Row 1 by 1/3.5
add a multiple of Row 1 to Row 2
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
multiply Row 0 by 0.5
1.00
0.00
0.00
1.50
3.50
-10.50
-0.50
0.50
4.50
-4.00
30.00
44.00
add 3 times Row 0 to Row 1
add -1 times Row 0 to Row 2
1.00
0.00
0.00
0.00
1.00
0.00
-0.71
0.14
6.00
-16.85
30.00
150.00
add a multiple of Row 1 to Row 0
multiply Row 1 by 1/3.5
add a multiple of Row 1 to Row 2
1.00
0.00
0.00
0.00
1.00
0.00
0.00
0.00
1.00
1.00
5.00
25.00
same for other columns
and so on...
Problem 2 -- “Life”
Grid World
John Conway
black cells are alive
Evolutionary rules
• Everything depends on a cell’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
black cells are alive
Evolutionary rules
• Everything depends on a cell’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
black cells are alive
Evolutionary rules
• Everything depends on a cell’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
Pair Program
black cells are alive
Evolutionary rules
• Everything depends on a cell’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Keep going!
life out there...
Problem 2 -- Creating Life
update(int[][] last, int[][] next)
new generation
old generation
0
1
2
3
4
0
5
0
0
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
Problem 2 -- Creating Life
update(int[][] last, int[][] next)
new generation
old generation
0
1
2
3
4
0
5
0
0
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
Problem 2 -- Details
update(int[][] last, int[][] next)
old generation
new generation
For each generation…
• 0 represents an empty cell
• 1 represents a living cell
• outermost edge should
always be left empty (even
if there are 3 neighbors)
• compute all cells based on
their previous neighbors
before updating any of them
http://www.math.com/students/wonders/life/life.html
Problem 2 -- Details
What will this do?
public static void update(int[][] last, int[][] next)
{
for (int r=1 ; r<last.length-1 ; ++r)
{
for (int c=1 ; c<last[r].length-1 ; ++c)
{
int oldvalue = last[r][c];
if (oldvalue == 0)
next[r][c] = 1;
else
next[r][c] = 0;
}
}
}
// look at last
// assign to next
Problem 2 -- Details
How does this change things?
public static void update(int[][] last, int[][] next)
{
for (int r=1 ; r<last.length-1 ; ++r)
{
for (int c=1 ; c<last[r].length-1 ; ++c)
{
int oldvalue = last[r-1][c-1];
if (oldvalue == 0)
next[r][c] = 1;
else
next[r][c] = 0;
}
}
}
Problem 2 – Multi-species Life (!)
updateMulti(int[][] last, int[][] next)
Create a set of rules to
evolve two species of
cells (plus empty).
The Challenge
Give both species a good
chance of survival in the
same environment.
0
1
more species are OK, too…
2 …
stability is an open biological question…
Lab: M-Z
Lab this week
• Problem 1: Gaussian Elimination
You’ll need to write (and use)
• Problem 2: Life and Multi-species Life!
0
1
2
3
4
0
5
0
0
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
printMenu
enterValues
print
multRow
addRowaToRowb
addMxRowaToRowb
solve
public void update(int[][] last, int[][] next)
public void updateMulti(int[][] last, int[][] next)
• Extra Credit:
A matrix-inverse feature for Problem 1 ...
Doughnut Life for Problem 2 …
A
“Quiz”
Starting with the 2d array A
shown here, what are the values
in A after running this code?
row 0
row 1
row 2
public static void mysteryMethod(int[][] A)
{
for (int r = 0 ; r < A.length ; ++r)
{
for (int c = 0 ; c < A[r].length ; ++c)
{
if (r == c)
{
A[r][c] = 42;
}
else
{
A[r][c] = A[r][c] + 1;
}
}
}
} // end of mystery method
Before
1
5
9
2
6
10
col 0
col 1
A
3
7
11
col 2
4
8
12
col 3
After
What are the resulting values in A?
“Quiz”
Write a method that
adds two times the
values in row #1 into
the values in row #2.
The values in row #1
should not change!
(The values in row #0
also don’t change!)
example
row 0
row 1
two of row 1
are to be
added to row 2
row 2
row 0
row 1
row 2
A
before
0.00
3.00
10.00
1.00
0.50
20.00
0.00
0.50
30.00
1.00
3.00
40.00
0.00
3.00
16.00
1.00
0.50
21.00
0.00
0.50
31.00
1.00
3.00
46.00
example
A
after
public static void addTwoOfRow1IntoRow2(double[][] A)
{
}
Watch out!
public static void main(String[] args)
{
H.out.println(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
int[] fav
fav[0]
fav[1]
H.out.println(fav + “ and ” + fav + “! Me too!”);
}
What will happen here?!
Watch out!
public static void main(String[] args)
{
H.out.println(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
int[] fav
fav[0]
fav[1]
H.out.println(fav[0] + “ and ” + fav[1] + “! Me too!”);
}
public static void conform(int[] fav)
{
fav = new int[2];
fav[0] = 42; fav[1] = 42;
return;
int[]
}
fav
This week in IS 313
A dizzying array of possibilities...
• HW 7 (2 problems)
due Thursday, 11/7 at midnight
Reading: Week 9’s online notes
• This week’s credits:
John Conway
Carl Gauss
Still seeking a costume?
Survival of the stablest
http://www.math.com/students/wonders/life/life.html
The Questions of Life...
• what are stable “life” forms?
• can they be unbounded spatially?
• can they grow forever?
• how densely can they grow?
• how fast can they travel?
“Pass By Value”
public static void main(String[] args)
{
H.out.println(“Welcome to Conformity, Inc.”);
int fav = 7;
// The user’s favorite #
conform(fav);
H.out.println(fav + “ is my favorite, too!”);
}
42 -100.1
3.14159 7
42 -100.1
3.14 7
42.00 -100.10
3.14 7.00
42.00
-100.10
3.14
7.00
42.00
-100.10
3.14
7.00
int age
Using 2d arrays
arr
Before
2.00
-3.00
1.00
3.00
-1.00
-9.00
-1.00
2.00
4.00
-8.00
42.00
56.00
arr
After
2.00
-3.00
1.00
3.00
-1.00
-9.00
-3.00
6.00
12.00
-8.00
42.00
56.00
A method for multiplying columns:
Solving ?!
2p
-3p
1p
+
+
+
3n
-1n
-9n
+
+
+
-1q
2q
4q
Goal: Find p,n,q
=
=
=
-8.00
42.00
56.00
Gaussian
Elimination
• get 1s along the diagonal
• get 0s elsewhere on the left
1.00p
-3.00p
1.00p
+
+
+
1.50n
-1.00n
-9.00n
+ -0.50q
+ 2.00q
+ 4.00q
=
=
=
-4.00
42.00
56.00
1.00p
0.00p
-0.00p
+
1.50n
+
3.50n
+ -10.50n
+ -0.50q
+ 0.50q
+ 4.50q
=
=
=
-4.00
30.00
44.00
multiply the zeroth
row (R0) by 0.5
add 3 times R0 to R1
add -1 times R0 to R2
Solved !
1.00p
0.00p
-0.00p
+
1.50n
+
3.50n
+ -10.50n
+ -0.50q
+ 0.50q
+ 4.50q
=
=
=
-4.00
30.00
44.00
multiply R1 by 1 / 3.5
1.00p
0.00p
-0.00p
+
1.50n
+
1.00n
+ -10.50n
+ -0.50q
+ 0.14q
+ 4.50q
=
=
=
-4.00
8.57
44.00
add -1.5 times R1 to R0
add 10.5 times R1 to R2
1.00p
0.00p
-0.00p
+
+
+
+ -0.71q
+ 0.14q
+ 6.00q
= -16.85
= 30.00
= 150.00
0.00n
1.00n
0.00n
add 3 times R0 to R1
add -1 times R0 to R2
continue...
1.00p
0.00p
-0.00p
+
+
+
0.00n
1.00n
0.00n
+
+
+
0.00q
0.00q
1.00q
=
=
=
1.00
5.00
25.00
only the array is
necessary !
Solved !
1.00p
0.00p
-0.00p
+
1.50n
+
3.50n
+ -10.50n
+ -0.50q
+ 0.50q
+ 4.50q
=
=
=
-4.00
30.00
44.00
multiply R1 by 1 / 3.5
1.00p
0.00p
-0.00p
+
1.50n
+
1.00n
+ -10.50n
+ -0.50q
+ 0.14q
+ 4.50q
=
=
=
-4.00
8.57
44.00
add -1.5 times R1 to R0
add 10.5 times R1 to R2
1.00p
0.00p
-0.00p
+
+
+
+ -0.71q
+ 0.14q
+ 6.00q
= -16.85
= 30.00
= 150.00
0.00n
1.00n
0.00n
add 3 times R0 to R1
add -1 times R0 to R2
continue...
1.00p
0.00p
-0.00p
+
+
+
0.00n
1.00n
0.00n
+
+
+
0.00q
0.00q
1.00q
=
=
=
1.00
5.00
25.00
only the array is
necessary !
This week in IS 313
A dizzying array of possibilities...
• HW 7 (2 problems)
due Thursday, 11/8 at midnight
Reading: Week 9’s online notes
Also: threads and Life !
John Conway
Carl Gauss
Still seeking a costume?
“Pass By Value”
public static void main(String[] args)
{
H.out.println(“Welcome to Conformity, Inc.”);
int fav = 7;
7
// The user’s favorite #
int fav
conform(fav);
H.out.println(fav + “ is my favorite, too!”);
PASS
BY
VALUE
}
public static void conform(int fav)
{
fav = 42;
}
7 42
int fav
“Pass by value” means that data is copied when sent to a method
Passing Arrays by Value
public static void main(String[] args)
{
H.out.println(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
H.out.println(fav[0] + “ and ” + fav[1] + “! Me too!”);
}
public static void conform(int[] fav)
{
fav[0] = 42; fav[1] = 42;
return;
}
Watch out!
public static void main(String[] args)
{
H.out.println(“Welcome to conformity, inc.”);
int[] fav = new int[2];
fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s
conform(fav);
H.out.println(fav[0] + “ and ” + fav[1] + “! Me too!”);
}
public static void conform(int[] fav)
{
fav = new int[2];
fav[0] = 42; fav[1] = 42;
return;
}
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Mathematicians don't care.
Creating structure from a
few simple facts...
?
Axioms
Definitions
20˚
Proof
60˚
10˚
70˚
Views of the world
Engineers think their equations are an approximation to reality.
Physicists think reality is an approximation to their equations.
Mathematicians don't care.
Creating structure from a
few simple facts...
Axioms
Definitions
Creating structure from a
few simple actions...
for
arrays
variables
while
arithmetic operations
if/else
Proof
Algorithm
Jagged arrays
Rows of 2d arrays need not be the same length!
double[][] arr;
arr = new double[3][];
arr[0] = new double[4];
arr[1] = new double[2];
arr[2] = new double[5];
double[][]
arr
double[]
arr[0]
double
double
double[]
arr[1]
double
double
double[]
arr[2]
double
double
double
double
double
double
double
Rectangular arrays
But there’s a shortcut to creating them if they are...
double[][] arr;
arr = new double[3][4];
double[][]
arr
double[]
arr[0]
double
double
double
double
double[]
arr[1]
double
double
double
double
double[]
arr[2]
double
double
double
double
2d arrays: Input
public static void main(String[] args)
{
double[][] arr = new int[3][4]; more likely to be
variables
for (int r=0 ; r<3 ; ++r)
{
for (int c=0 ; c<4 ; ++c)
{
arr[r][c] = H.in.nextDouble();
}
}
no i or j ?
2d arrays: Input Method
public static void enterArray(double[][] arr)
{
how do we find the
number of rows and the
number of columns?
for (int r=0 ; r <
; ++r)
number of rows
{
for (int c=0 ; c <
; ++c)
number of columns
{
arr[r][c] = H.in.nextDouble();
}
}
}
Problem 1
An array of array handlers...
Initial Setup
Get the number of rows and columns
Create an array of the appropriate number of elements (doubles).
Get initial values from the user into the array.
Menu
(0) Change the values in the array
(1) Print the array
(2) Multiply an array row
(3) Add one row to another
(4) Add a multiple of one row to another
(5) Solve!
printMenu
(9) Quit
addMR1ToR2
Which choice would you like?
solve
enterArray
printArray
multRow
addR1ToR2
Methods
Adding one row to another...
arr Before
2.00
-3.00
1.00
3.00
-1.00
-9.00
arr After
-1.00
2.00
4.00
-8.00
42.00
56.00
2.00
-3.00
3.00
3.00
-1.00
-6.00
-1.00
2.00
3.00
-8.00
42.00
48.00
public static void addR1ToR2(double[][] arr, int rFrom, int rTo)
{
}
Gaussian Elimination
Carl Gauss is
so money!
2p
-3p
1p
+
+
+
3n
-1n
-9n
+
+
+
2.00
-3.00
1.00
+
+
+
3.00
-1.00
-9.00
0n
1n
0n
=
=
=
-8.00
42.00
56.00
• get 1s along the diagonal
• get 0s elsewhere on the left
Goal: Find p,n,q
1p
0p
0p
-1q
2q
4q
+
+
+
-1.00
2.00
4.00
0q
0q
1q
=
=
=
-8.00
42.00
56.00
1.00
5.00
25.00
Only the array is necessary !
We can get rid of the variables...
Solve
1.00
-3.00
1.00
1.50
-1.00
-9.00
-0.50
2.00
4.00
-4.00
42.00
56.00
multiply Row 0 by 0.5
1.00
0.00
0.00
1.50
3.50
-10.50
-0.50
0.50
4.50
-4.00
30.00
44.00
add 3 times Row 0 to Row 1
add -1 times Row 0 to Row 2
1.00
0.00
0.00
0.00
1.00
0.00
-0.71
0.14
6.00
-16.85
30.00
150.00
repeat for column 2
1.00
0.00
0.00
0.00
1.00
0.00
0.00
0.00
1.00
1.00
5.00
25.00
and column 3
and so on...
Problem 2 -- “Life”
Grid World
John Conway
black cells are alive
Evolutionary rules
• Everything depends on one’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
black cells are alive
Evolutionary rules
• Everything depends on one’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
black cells are alive
Evolutionary rules
• Everything depends on one’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Problem 2 -- Life
Grid World
black cells are alive
Evolutionary rules
• Everything depends on one’s
eight neighbors
• Exactly 3 neighbors give birth
to a new, live cell!
• Exactly 2 or 3 neighbors keep an
existing cell alive
• Any other number of neighbors kill
the central cell (or keep it dead)
white cells are empty
Keep going!
life out there...
Problem 2 -- Creating Life
public void update(int[][] last, int[][] next)
new generation
old generation
0
1
2
3
4
0
5
0
0
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
Problem 2 -- Creating Life
public void update(int[][] last, int[][] next)
new generation
old generation
0
1
2
3
4
0
5
0
0
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
Problem 2 -- Details
public void update(int[][] last, int[][] next)
old generation
new generation
For each generation…
• 0 represents an empty cell
• 1 represents a living cell
• outermost edge should
always be empty (even if
there are 3 neighbors)
• compute all cells based on
their previous neighbors
before updating any of them
http://www.math.com/students/wonders/life/life.html