HMC’s Galileo Auditorium Some of Cope’s generated MP3s: Experiments in Musical Intelligence 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. 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
Still seeking a costume? Week 9 in CS 5 Lab: M-Z A dizzying array of possibilities… HW 9 (2 probs) due Sunday, 10/31 at midnight M/T due Monday, 11/1 at midnight 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” 7 public static void main(String[] args) { H.pl(“Welcome to Conformity, Inc.”); int fav = 7; // The user’s favorite # conform(fav); H.pl(fav + “ is my favorite, too!”); } public static void conform(int fav) fav = 42; return; 7 int fav int fav
“Pass by value” means that data is copied when sent to a method public static void main(String[] args) { H.pl(“Welcome to Conformity, Inc.”); int fav = 7; // The user’s favorite # conform(fav); H.pl(fav + “ is my favorite, too!”); } public static void conform(int fav) fav = 42; return; 7 int fav PASS BY VALUE 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); H.pl(fav[0] + “ and ” + fav[1] + “! Me too!”); } public static void conform(int[] fav) fav[0] = 42; fav[1] = 42; return; int[] fav fav[0] fav[1] 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); H.pl(fav[0] + “ and ” + fav[1] + “! Me too!”); } public static void conform(int[] fav) fav[0] = 42; fav[1] = 42; return; int[] fav fav[0] fav[1] can change data elsewhere! int[] 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.
Creating structure from a few simple facts... 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˚ 10˚ 60˚ Proof 70˚
The John Conway Challenge…. ? 20˚ 10˚ 60˚ 70˚ (without using trig)
Views of the world Proof Algorithm 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 actions ... Creating structure from a few simple facts... for arrays variables Axioms while Definitions arithmetic operations if/else Proof Algorithm
they don’t have to be horizontal lists! Arrays’ flexibility Arrays can be of ANY type double[] A; 42.0 75.0 70.0 A = new double[3]; double[] double double double A they don’t have to be horizontal lists!
they don’t have to be horizontal lists! Arrays’ flexibility Arrays can be of ANY type double[] A; 42.0 75.0 70.0 A = new double[3]; double[] double double double A 42.0 double[] double they don’t have to be horizontal lists! A 75.0 double 70.0 double
Arrays’ flexibility Arrays can be of ANY type double[] A; int[] A; 42.0 75.0 70.0 A = new double[3]; double[] double double double A int[] A; 42 7 -11 A = new int[3]; int[] int int int A String[] A; “go” “red” “sox!” A = new String[3]; String[] String String String A
2d arrays Arrays can be of ANY type -- so what type of array is A ? double[][] A; double[][] A
A is an array of double arrays! 2d arrays Arrays can be of ANY type -- even other arrays! double[][] A; A is an array of double arrays! double[][] A
2d arrays Arrays can be of ANY type -- even other arrays! double[][] A; A = new double[3][]; double[] double[][] A[0] A 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[] double[][] A[0] double double double double A double[] A[1] double double double[] A[2] double double double double double A[0] = new double[4]; A[1] = new double[2]; A[2] = new double[5];
We will not use jagged arrays Rows of 2d arrays need not be the same length! double[][] A; A = new double[3][]; double[] double[][] A[0] double double double double A double[] A[1] double double double[] A[2] double double double double double A[0] = new double[4]; A[1] = new double[2]; A[2] = new double[5];
Rectangular arrays But there’s a shortcut to creating them if they are... double[][] A; rows cols A = new double[3][4]; double[] double[][] double double double double A[0] A double[] double double double double A[1] double[] A[2] double double double double
double[][] A = new double[3][4]; 2d arrays Getting at individual elements: rows cols double[][] A = new double[3][4]; double[] double[][] double double double double A[0] A A[0][0] double[] double double double double A[1] double[] A[2] double double double double A[2][3] A[1][2] = 10.0
these will be variables 2d arrays: Input public static void main(String[] args) { double[][] A = new double[3][4]; H.pl(“Input your values:”) for (int r=0 ; r<3 ; ++r) for (int c=0 ; c<4 ; ++c) A[r][c] = H.nd(); } these will be variables no i or j ?
how do we find the number of rows and the number of columns? 2d arrays: Input Method public static void enterValues(double[][] A) { H.pl(“Input your values:”); for (int r=0 ; r < ; ++r) for (int c=0 ; c < ; ++c) A[r][c] = H.nd(); } how do we find the number of rows and the number of columns? number of rows number of columns
number of columns in row r 2d arrays: Output public static void print(double[][] A) { for (int r = 0; r < A.length; ++r) for (int c = 0; c < A[r].length; ++c) H.p( } number of rows number of columns in row r I/O formatting reference: http://www.cs.hmc.edu/courses/2004/fall/cs5/HMCSupport.html
Problem 1 An array of array handlers... Initial Setup Menu Methods 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. printMenu enterValues print multRow addRowaIntoRowb addMxRowaIntoRowb solve 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! (9) Quit Which choice would you like? Methods
What are the resulting values in A? Before “Quiz” 1 2 3 4 5 6 7 8 9 10 11 12 row 0 Starting with the 2d array A shown here, what are the values in A after running this code? row 1 row 2 col 0 col 1 col 2 col 3 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 A After What are the resulting values in A?
“Quiz” A A example before example after 0.00 1.00 0.00 1.00 row 0 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00 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!) row 1 two of row 1 are to be added to row 2 row 2 row 0 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00 row 1 row 2 A example after public static void addTwoOfRow1IntoRow2(double[][] A) { }
Adding one row to another... Before A After 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 3.00 -6.00 3.00 48.00 public static void addRowaIntoRowb(double[][] A, int ra, int rb) { }
Adding one row to another... Before A After 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 3.00 -6.00 3.00 48.00 2 public static void addRowaIntoRowb(double[][] A, int ra, int rb) { }
Problem 1 An array of array handlers... Initial Setup Menu Methods 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. printMenu enterValues print multRow addRowaIntoRowb addMxRowaIntoRowb solve 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! (9) Quit Which choice would you like? Methods
Gaussian Elimination Goal: Find p,n,q 2p + 3n + -1q = -8.00 Carl Gauss is so money! 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 Goal: Find p,n,q
Gaussian Elimination Goal: get 1s along the diagonal Find p,n,q get 0s elsewhere on the left 1p + 0n + 0q = 1.00 0p + 1n + 0q = 5.00 0p + 0n + 1q = 25.00
Gaussian Elimination Goal: get 1s along the diagonal Find p,n,q Carl Gauss is money. 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 get 1s along the diagonal Goal: Find p,n,q get 0s elsewhere on the left 1p + 0n + 0q = 1.00 0p + 1n + 0q = 5.00 0p + 0n + 1q = 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 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5
a hint as to the direction to head next… ! Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 a hint as to the direction to head next… !
add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2
add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.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
add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.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 1.00 0.00 1.00 0.00 5.00 0.00 0.00 1.00 25.00 same for other columns and so on...
Problem 2 -- “Life” Grid World black cells are alive John Conway 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 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) Keep going! white cells are empty life out there...
Problem 2 -- Creating Life update(int[][] last, int[][] next) new generation old generation 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
Problem 2 -- Creating Life update(int[][] last, int[][] next) new generation old generation 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
Problem 2 -- Details update(int[][] last, int[][] next) new generation old 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) // look at last next[r][c] = 1; // assign to next else next[r][c] = 0; }
How does this change things? 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. more species are OK, too… 1 2 … stability is an open biological question…
Lab this week Lab: M-Z Problem 1: Gaussian Elimination printMenu enterValues print multRow addRowaToRowb addMxRowaToRowb solve You’ll need to write (and use) Problem 2: Life and Multi-species Life! 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5 public void update(int[][] last, int[][] next) public void updateMulti(int[][] last, int[][] next) A matrix-inverse feature for Problem 1 ... Extra Credit: Doughnut Life for Problem 2 …
What are the resulting values in A? Before “Quiz” 1 2 3 4 5 6 7 8 9 10 11 12 row 0 Starting with the 2d array A shown here, what are the values in A after running this code? row 1 row 2 col 0 col 1 col 2 col 3 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 A After What are the resulting values in A?
“Quiz” A A example before example after 0.00 1.00 0.00 1.00 row 0 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00 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!) row 1 two of row 1 are to be added to row 2 row 2 row 0 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00 row 1 row 2 A example 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); H.out.println(fav + “ and ” + fav + “! Me too!”); } int[] fav fav[0] fav[1] 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); 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 fav[0] fav[1] int[] fav
Still seeking a costume? 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 Still seeking a costume? Carl Gauss
Survival of the stablest http://www.math.com/students/wonders/life/life.html The Questions of Life... what are stable “life” forms? how densely can they grow? can they be unbounded spatially? how fast can they travel? can they grow forever?
“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
A method for multiplying columns: Using 2d arrays arr 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 Before arr 2.00 3.00 -3.00 -8.00 -3.00 -1.00 6.00 42.00 1.00 -9.00 12.00 56.00 After A method for multiplying columns: MONUMENTAL COLUMNS HANDLED MONUMENTALLY WELL. The eight massive Corinthian columns at the National Building Museum in Washington, D.C., are among the largest interior columns in the world: 75 ft. high and 8 ft. in dia., the columns have a brick core (over 70,000 bricks each!) covered with stucco.
Solving ?! Gaussian Elimination Goal: get 1s along the diagonal 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 Gaussian Elimination get 1s along the diagonal Goal: Find p,n,q get 0s elsewhere on the left 1.00p + 1.50n + -0.50q = -4.00 -3.00p + -1.00n + 2.00q = 42.00 1.00p + -9.00n + 4.00q = 56.00 multiply the zeroth row (R0) by 0.5 1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 -0.00p + -10.50n + 4.50q = 44.00 add 3 times R0 to R1 add -1 times R0 to R2
Solved ! 1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 multiply R1 by 1 / 3.5 1.00p + 1.50n + -0.50q = -4.00 0.00p + 1.00n + 0.14q = 8.57 -0.00p + -10.50n + 4.50q = 44.00 add -1.5 times R1 to R0 add 10.5 times R1 to R2 1.00p + 0.00n + -0.71q = -16.85 0.00p + 1.00n + 0.14q = 30.00 -0.00p + 0.00n + 6.00q = 150.00 add 3 times R0 to R1 add -1 times R0 to R2 continue... 1.00p + 0.00n + 0.00q = 1.00 0.00p + 1.00n + 0.00q = 5.00 -0.00p + 0.00n + 1.00q = 25.00 only the array is necessary !
Solved ! 1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 multiply R1 by 1 / 3.5 1.00p + 1.50n + -0.50q = -4.00 0.00p + 1.00n + 0.14q = 8.57 -0.00p + -10.50n + 4.50q = 44.00 add -1.5 times R1 to R0 add 10.5 times R1 to R2 1.00p + 0.00n + -0.71q = -16.85 0.00p + 1.00n + 0.14q = 30.00 -0.00p + 0.00n + 6.00q = 150.00 add 3 times R0 to R1 add -1 times R0 to R2 continue... 1.00p + 0.00n + 0.00q = 1.00 0.00p + 1.00n + 0.00q = 5.00 -0.00p + 0.00n + 1.00q = 25.00 only the array is necessary !
Still seeking a costume? 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 Still seeking a costume? Carl Gauss
“Pass by value” means that data is copied when sent to a method 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!”); } public static void conform(int fav) fav = 42; 7 int fav PASS BY VALUE 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;
Creating structure from a few simple facts... 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˚ 10˚ 60˚ Proof 70˚
Views of the world Proof Algorithm 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 actions... Creating structure from a few simple facts... for arrays variables Axioms while Definitions 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[] double[][] double double double double arr[0] arr double[] double double arr[1] double[] arr[2] 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[] double[][] double double double double arr[0] arr double[] double double double double arr[1] double[] arr[2] double double double double
more likely to be variables 2d arrays: Input public static void main(String[] args) { double[][] arr = new int[3][4]; for (int r=0 ; r<3 ; ++r) for (int c=0 ; c<4 ; ++c) arr[r][c] = H.in.nextDouble(); } more likely to be variables no i or j ?
how do we find the number of rows and the number of columns? 2d arrays: Input Method public static void enterArray(double[][] arr) { for (int r=0 ; r < ; ++r) for (int c=0 ; c < ; ++c) arr[r][c] = H.in.nextDouble(); } how do we find the number of rows and the number of columns? number of rows number of columns
Problem 1 An array of array handlers... Initial Setup Menu Methods 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. printMenu enterArray printArray multRow addR1ToR2 addMR1ToR2 solve 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! (9) Quit Which choice would you like? Methods
Adding one row to another... arr Before arr After 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 3.00 -6.00 3.00 48.00 public static void addR1ToR2(double[][] arr, int rFrom, int rTo) { }
Only the array is necessary ! We can get rid of the variables... Gaussian Elimination Carl Gauss is so money! 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 get 1s along the diagonal Goal: Find p,n,q get 0s elsewhere on the left 1p + 0n + 0q = 1.00 0p + 1n + 0q = 5.00 0p + 0n + 1q = 25.00 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 Only the array is necessary ! We can get rid of the variables...
add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.00 150.00 repeat for column 2 1.00 0.00 0.00 1.00 0.00 1.00 0.00 5.00 0.00 0.00 1.00 25.00 and column 3 and so on...
Problem 2 -- “Life” Grid World black cells are alive John Conway 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
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) Keep going! white cells are empty life out there...
Problem 2 -- Creating Life public void update(int[][] last, int[][] next) new generation old generation 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
Problem 2 -- Creating Life public void update(int[][] last, int[][] next) new generation old generation 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
Problem 2 -- Details public void update(int[][] last, int[][] next) new generation old 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