Exercise Sheet 1 - Solutions Alessandro Gnoatto October 23, 2014 1 Exercise 1 / 2 4 package de. lmu. firstclass ; 6 / @author Alessandro Gnoatto 8 10 public class ComplexNumber { //fields 12 private double real ; private double imag ; 14 16 //specify a constructor public ComplexNumber (double real, double imag ){ 18 this. real = real ; this. imag = imag ; 20 22 //get methods public double getreal (){ 24 return this. real ; 26 public double getimag (){ 28 return this. real ; 30 //set methods 32 public void setreal (double real ){ this. real = real ; 34 36 public void setimag (double imag ){ this. imag = imag ; 38 40 //some methods public s t a t i c ComplexNumber add ( ComplexNumber a, ComplexNumber b){ 42 return new ComplexNumber (a. real +b.real,a. imag +b. imag ); 44 1
public s t a t i c ComplexNumber subtract ( ComplexNumber a, ComplexNumber b){ 46 return new ComplexNumber (a.real -b.real,a.imag -b. imag ); 48 public s t a t i c ComplexNumber multiply ( ComplexNumber a, ComplexNumber b){ 50 return new ComplexNumber ( a. real * b. real - a. imag * b. imag,a. imag * b. real + a. real * b. imag ); 52 54 public s t a t i c ComplexNumber divide ( ComplexNumber a, ComplexNumber b){ return new ComplexNumber (( a. real * b. real + a. imag * b. imag ) 56 /(b. real * b. real - b. imag * b. imag ), (a. imag * b. real - a. real * b. imag ) 58 /(b. real * b. real - b. imag * b. imag )); 60 public s t a t i c double abs ( ComplexNumber a){ 62 return Math. sqrt (a. real *a. real +a. imag *a. imag ); 64 66 2 Exercise 2 2.1 Interface / 2 (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christian fries.de. 4 Created on 23.11.2012 6 package net. finmath. time ; 8 import java. util. ArrayList ; 10 / @author Christian Fries 12 public interface TimeDiscretizationInterface extends Iterable < Double > { 14 / 16 @return Returns the number of time discretization points. 18 int getnumberoftimes (); 20 / @return Returns the number of time steps (= number of discretization points 1). 22 int getnumberoftimesteps (); 24 / 26 Returns the time for the given time index. 2
28 @param timeindex Time index @return Returns the time for a given time index. 30 double gettime ( int timeindex ); 32 / 34 Returns the time step from the given time index to the next one. 36 @param timeindex Time index @return Returns the time step 38 double gettimestep ( int timeindex ); 40 / 42 Returns the time index for the given time. If the given time is not in the time discretization the method returns a negative number being ( insertionpoint 1). 44 @param time The time. 46 @return Returns the time index for a given time. 48 int gettimeindex (double time ); 50 / Returns the time index for the time in the time discretization which is the nearest 52 to the given time, being less or equal (i.e. max(i : timediscretization[i] time where timediscretization[i] timediscretization[j]). 54 @param time Given time. 56 @return Returns a time index 58 int gettimeindexnearestlessorequal (double time ); 60 / Returns the time index for the time in the time discretization which is the nearest 62 to the given time, being greater or equal (i.e. min(i : timediscretization[i] time where timediscretization[i] timediscretization[j]). 64 @param time Given time. 66 @return Returns a time index 68 int gettimeindexnearestgreaterorequal (double time ); 70 / Return a clone of this time discretization as <code> double[]</code>. 72 @return The time discretization as <code>double[]</code> 74 double[] getasdoublearray (); 76 / Return a clone of this time discretization as <code> ArrayList<Double></code>. 78 Note that this method is costly in terms of performance. 3
80 @return The time discretization as <code>arraylist< Double></code> 82 ArrayList < Double > getasarraylist (); 84 / 86 Return a new time discretization where all time points have been shifted by a given time shift. 88 @param timeshift A time shift applied to all discretization points. 90 @return A new time discretization where all time points have been shifted by the given time shift. 92 TimeDiscretizationInterface gettimeshiftedtimediscretization (double timeshift ); 2.2 Class / 2 (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christian fries.de. 4 Created on 20.04.2008 6 package net. finmath. time ; 8 import java. io. Serializable ; import java. util. ArrayList ; 10 import java. util. Arrays ; import java. util. Iterator ; 12 / 14 This class represents a set of discrete points in time. <br> 16 It handles the mapping from time indices to time points and back. It uses a time tick size ("quantum") of 1.0 / (365.0 24.0) (which corresponds to one hour if 1.0 is a non leap year): 18 Times are rounded to the nearest multiple of 1.0 / (365.0 24.0). 20 Objects of this class are immutable. 22 @author Christian Fries @version 1.5 24 public class TimeDiscretization implements Serializable, TimeDiscretizationInterface { 26 28 private s t a t i c f i n a l long serialversionuid = 6880668325019167781 L; private f i n a l double[] timediscretization ; 30 private f i n a l double timeticksize = 1.0 / (365.0 * 24.0) ; 32 public enum ShortPeriodLocation { 4
SHORT_PERIOD_AT_START, 34 SHORT_PERIOD_AT_END 36 / 38 Constructs a time discretization from a given set of doubles. 40 @param times Given array or arguments list of discretization points. 42 public TimeDiscretization (double... times ) { super(); 44 this. timediscretization = times. clone (); java. util. Arrays. sort ( this. timediscretization ); 46 48 / Constructs a time discretization from a given set of Doubles. 50 @param times Given array or arguments list of discretization points 52 public TimeDiscretization ( Double [] times ) { 54 super(); this. timediscretization = new double[ times. length ]; 56 for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) this. timediscretization [ timeindex ] = roundtotimeticksize ( times [ timeindex ]); java. util. Arrays. sort ( this. timediscretization ); 58 60 / Constructs a time discretization from a given ArrayList of Doubles. 62 @param timediscretization Given ArrayList of discretization points 64 public TimeDiscretization ( ArrayList < Double > timediscretization ) { 66 super(); this. timediscretization = new double[ timediscretization. size () ]; 68 for ( int timeindex =0; timeindex < timediscretization. size (); timeindex ++) this. timediscretization [ timeindex ] = roundtotimeticksize ( timediscretization. get ( timeindex )) ; java. util. Arrays. sort ( this. timediscretization ); 70 72 / Constructs an equi distant time discretization with points timediscretization[i] being 74 <code>for(i=0; i timesteps; i++) timediscretization[ i] = initial + i deltat;</code> 76 @param initial First discretization point. @param numberoftimesteps Number of time steps. 78 @param deltat Time step size. 5
80 public TimeDiscretization (double initial, int numberoftimesteps, double deltat ) { super(); 82 timediscretization = new double[ numberoftimesteps +1]; for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( initial + timeindex * deltat ); 84 86 / Constructs an equi distant time discretization with stub periods at start or end. 88 @param initial First discretization point. 90 @param last Last time steps. @param deltat Time step size. 92 @param shortperiodlocation Placement of the stub period. 94 public TimeDiscretization (double initial, double last, double deltat, ShortPeriodLocation shortperiodlocation ) { super(); 96 int numberoftimesteps = ( int )(( last - initial )/ deltat + 0.5) ; 98 // Adjust for short period, if any i f ( roundtotimeticksize ( initial + numberoftimesteps * deltat ) < roundtotimeticksize ( last )) numberoftimesteps ++; 100 timediscretization = new double[ numberoftimesteps +1]; 102 i f ( shortperiodlocation == ShortPeriodLocation. SHORT_PERIOD_AT_END ) { for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( initial + timeindex * deltat ); 104 timediscretization [ timediscretization. length -1] = last ; 106 else { for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( last - ( numberoftimesteps - timeindex ) * deltat ); 108 timediscretization [0] = initial ; 110 112 @Override public int getnumberoftimes () { 114 return timediscretization. length ; 116 @Override 118 public int getnumberoftimesteps () { return timediscretization. length -1; 120 122 @Override public double gettime ( int timeindex ) { 124 return timediscretization [ timeindex ]; 126 6
@Override 128 public double gettimestep ( int timeindex ) { return timediscretization [ timeindex +1] - timediscretization [ timeindex ]; 130 132 @Override public int gettimeindex (double time ) { 134 int index = java. util. Arrays. binarysearch ( timediscretization, roundtotimeticksize ( time )); return index ; 136 138 @Override public int gettimeindexnearestlessorequal (double time ) { 140 int index = java. util. Arrays. binarysearch ( timediscretization, roundtotimeticksize ( time )); i f ( index < 0) index = - index -2; 142 return index ; 144 @Override 146 public int gettimeindexnearestgreaterorequal (double time ) { int index = java. util. Arrays. binarysearch ( timediscretization, time ); 148 i f ( index < 0) index = - index -1; return index ; 150 152 @Override public double[] getasdoublearray () { 154 // Note: This is a deep copy return timediscretization. clone (); 156 158 @Override public ArrayList < Double > getasarraylist () { 160 ArrayList < Double > times = new ArrayList < Double >( timediscretization. length ); for (double atimediscretization : timediscretization ) times. add ( atimediscretization ); 162 return times ; 164 @Override 166 public TimeDiscretizationInterface gettimeshiftedtimediscretization (double timeshift ) { double[] newtimediscretization = new double[ timediscretization. length ]; 168 for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) newtimediscretization [ timeindex ] = roundtotimeticksize ( timediscretization [ timeindex ]+ timeshift ); 170 java. util. Arrays. sort ( this. timediscretization ); return new TimeDiscretization ( newtimediscretization ); 172 174 @Override public Iterator < Double > iterator () { 176 return this. getasarraylist (). iterator (); 178 7
@Override 180 public String tostring () { return " TimeDiscretization [ timediscretization =" 182 + Arrays. tostring ( timediscretization ) + ", timeticksize =" + timeticksize + "]"; 184 186 private double roundtotimeticksize (double time ) { return Math. rint ( time / timeticksize )* timeticksize ; 188 8