Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Arrays, Vectors , and Matrices Thursday 14th 2010
Thursday, October 14, 2010
Types of Arrays
Class/Type Construction and Size query Remarks
array
Double[] x = new Double[2];x[0] = 5.0;x[1] = 4.3;int size = x.length;
arrays cannot be extended once they are defined, these are rather dumb containers, but fast to access.
ArrayList
ArrayList x = new ArrayList();x.add(object1);x.add(object2);int size = x.size();
Elements in ArrayList need to be instances of classes, for example Double object1 = new Double(2.0)
Vector
Vector x = new Vector();x.add(object1);x.add(object2);int size = x.size();
Vector is a synonym of ArrayList and is phasing out.
Thursday, October 14, 2010
Looping over Arrays
// x is an ArrayListDouble sum = new Double(0.0);Iterator xit = x.iterator();while (xit.hasNext()) { sum += (Double) xit.next();}
// x is and ArrayListDouble sum = new Double(0.0);int i;for(i=0; i < x.size();i++){ sum += (Double) x.get();}
// x is an ArrayListDouble sum = new Double(0.0);for(Object element : x){ sum += (Double) element;}
// x is a Double[] arrayDouble sum = new Double(0.0);int i;for(i=0; i < x.size();i++){ sum += (Double) x.get();}
// x is a Double[] arrayDouble sum = new Double(0.0);for(Double element : x){ sum += element;}
No iterator for array
for for
foreach
foreach
foreachforeach
while
Thursday, October 14, 2010
Better use of ArrayList
ArrayList x = new ArrayList();Double value = new Double(5.0);x.add(value);
ArrayList evolved in recent Java version to use a Template in its initialization.We used simply
This is unsafe because we should make clear that we deal with Doubleand we can use a template mechanism:
ArrayList x = new ArrayList<Double>();Double value = new Double(5.0);x.add(value);
In the textbook you will see this often in the general form as ArrayList<T>, where Tis a Type, like Double, Int, or Long etc.
Thursday, October 14, 2010
IteratorsTechnically, all indicators into an array are iterators, a device that helps us to orderly accessthe elements of an array.
In Java Iterator are more narrowly defined as a tool that can access a container class, for example an ArrayList (but there are many others: List, Vector, Hash, ...), in an orderly fashion, for example from the first to the last, even when the container class does not really allow standard indexing (like a hash). A container class MUST define a method iterator() that supplies the iterator.
Iterators come in two flavors: Iterator (very general but can only go in one direction), and ListIterator, that can go back and forth in the container and also can manipulate its content.
// x is an ArrayListDouble sum = new Double(0.0);Iterator<Double> xit = x.iterator();while (xit.hasNext()) { sum += (Double) xit.next();}
// x is an ArrayListDouble sum = new Double(0.0);ListIterator<Double> xit = x.listiterator();while (xit.hasNext()) { sum += (Double) xit.next();}
Thursday, October 14, 2010
Multidimensional arrays
ArrayList<ArrayList> x = new ArrayList<ArrayList>()for(i=0;i<3;i++){
ArrayList<Int> y = new ArrayList<Int>()for( i=0; i<2; i++){
y.add(i);}x.add(y);
}
[ [ 1, 2 ] , [ 1, 2 ] ]
Thursday, October 14, 2010
Matrix multiplication
Thursday, October 14, 2010
Regression analysis
Thursday, October 14, 2010
Regression analysisx = ((1, 6), (2, 5), (3, 7), (4, 10))y = α+ βx
6 = α+ β1
5 = α+ β2
7 = α+ β7
10 = α+ β4
S(α,β) = (6− (α+ β1))2+
(5− (α+ β2))2+
(7− (α+ β7))2+
(10− (α+ β4))2
We now minimize S, using partial derivatives for and using partial derivatives. This leads to the solution
α β
S(3.5, 1.4) = 1.12 + (−1)2 + (−0.7)2 + 0.92
Thursday, October 14, 2010
Regression analysis
Thursday, October 14, 2010
Regression analysis
Thursday, October 14, 2010
Regression analysis
Thursday, October 14, 2010
Use of “foreign” library or filesMany other Java sources are out there, for example - math libraries- plot libraries
We will look at the common math library by apache: http://commons.apache.org/math/index.html
Wikipedia definition of JAR files:A JAR file (or Java ARchive) aggregates many files into one.[1] Software developers generally use .jar files to distribute Java applications or libraries, in the form of classes and associated metadata and resources (text, images, etc.). JAR files build on the ZIP file format. Computer users can create or extract JAR files using the jar command that comes with a JDK. They can also use zip tools to do so; however, caution should be exercised as to the order of entries in the zip file headers as the manifest likely needs to be first.
Thursday, October 14, 2010
Installation into netbeans
If the distribution contains jar files for the classes, and the javadoc documentation, then the easiest way is to add the files to the Library by using the Tools menu in netbeans.
Thursday, October 14, 2010
Regression analysis using commons-math
Thursday, October 14, 2010