1  using System;


2  using System.Diagnostics;


3 


4  namespace HeuristicLab.Problems.DynamicalSystemsModelling {


5  public class Vector {


6  public readonly static Vector Zero = new Vector(new double[0]);


7 


8  public static Vector operator +(Vector a, Vector b) {


9  if (a == Zero) return b;


10  if (b == Zero) return a;


11  Trace.Assert(a.arr.Length == b.arr.Length);


12  var res = new double[a.arr.Length];


13  for (int i = 0; i < res.Length; i++)


14  res[i] = a.arr[i] + b.arr[i];


15  return new Vector(res);


16  }


17 


18  public static Vector AddTo(Vector a, Vector b) {


19  if (b == Zero) return a;


20  if (a == Zero) {


21  var vArr = new double[b.Length];


22  b.CopyTo(vArr);


23  return new Vector(vArr);


24  } else {


25  Trace.Assert(a.arr.Length == b.arr.Length);


26  for (int i = 0; i < a.arr.Length; i++)


27  a.arr[i] += b.arr[i];


28  return a;


29  }


30  }


31 


32 


33  public static Vector Subtract(Vector a, Vector b) {


34  if (b == Zero) return a;


35  if (a == Zero) {


36  var vArr = new double[b.Length];


37  a = new Vector(vArr);


38  }


39  Trace.Assert(a.arr.Length == b.arr.Length);


40  for (int i = 0; i < a.arr.Length; i++)


41  a.arr[i] = b.arr[i];


42  return a;


43  }


44 


45  public static Vector operator (Vector a, Vector b) {


46  if (b == Zero) return a;


47  if (a == Zero) return b;


48  Trace.Assert(a.arr.Length == b.arr.Length);


49  var res = new double[a.arr.Length];


50  for (int i = 0; i < res.Length; i++)


51  res[i] = a.arr[i]  b.arr[i];


52  return new Vector(res);


53  }


54  public static Vector operator (Vector v) {


55  if (v == Zero) return Zero;


56  for (int i = 0; i < v.arr.Length; i++)


57  v.arr[i] = v.arr[i];


58  return v;


59  }


60 


61  public static Vector operator *(double s, Vector v) {


62  if (v == Zero) return Zero;


63  if (s == 0.0) return Zero;


64  var res = new double[v.arr.Length];


65  for (int i = 0; i < res.Length; i++)


66  res[i] = s * v.arr[i];


67  return new Vector(res);


68  }


69 


70  public Vector Scale(double s) {


71  if (this != Zero) {


72  for (int i = 0; i < arr.Length; i++) {


73  arr[i] *= s;


74  }


75  }


76 


77  return this;


78  }


79 


80  public static Vector operator *(Vector v, double s) {


81  return s * v;


82  }


83  public static Vector operator *(Vector u, Vector v) {


84  if (v == Zero) return Zero;


85  if (u == Zero) return Zero;


86  var res = new double[v.arr.Length];


87  for (int i = 0; i < res.Length; i++)


88  res[i] = u.arr[i] * v.arr[i];


89  return new Vector(res);


90  }


91  public static Vector operator /(double s, Vector v) {


92  if (s == 0.0) return Zero;


93  if (v == Zero) throw new ArgumentException("Division by zero vector");


94  var res = new double[v.arr.Length];


95  for (int i = 0; i < res.Length; i++)


96  res[i] = 1.0 / v.arr[i];


97  return new Vector(res);


98  }


99  public static Vector operator /(Vector v, double s) {


100  return v * 1.0 / s;


101  }


102 


103  public static Vector Sin(Vector s) {


104  var res = new double[s.arr.Length];


105  for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]);


106  return new Vector(res);


107  }


108  public static Vector Cos(Vector s) {


109  var res = new double[s.arr.Length];


110  for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]);


111  return new Vector(res);


112  }


113 


114  public double this[int idx] {


115  get {


116  if (this == Vector.Zero) return 0.0;


117  else return arr[idx];


118  }


119  set {


120  if (this == Vector.Zero) throw new InvalidOperationException();


121  else arr[idx] = value;


122  }


123  }


124 


125  public int Length {


126  get {


127  if (this == Vector.Zero) throw new InvalidOperationException();


128  else return arr.Length;


129  }


130  }


131 


132  private readonly double[] arr; // backing array;


133 


134  /// <summary>


135  ///


136  /// </summary>


137  /// <param name="v">Is not cloned!</param>


138  public Vector(double[] v) {


139  this.arr = v;


140  }


141 


142  public void CopyTo(double[] target) {


143  Trace.Assert(arr.Length <= target.Length);


144  Array.Copy(arr, target, arr.Length);


145  }


146 


147 


148  /// <summary>


149  /// Creates a new vector


150  /// </summary>


151  /// <param name="a"> is cloned</param>


152  /// <returns></returns>


153  public static Vector CreateNew(double[] a) {


154  var arr = new double[a.Length];


155  Array.Copy(a, arr, arr.Length);


156  return new Vector(arr);


157  }


158 


159  /// <summary>


160  /// Creates a new vector


161  /// </summary>


162  /// <param name="v"> is cloned</param>


163  /// <returns></returns>


164  public static Vector CreateNew(Vector v) {


165  if (v == Zero) return Zero;


166  var arr = new double[v.arr.Length];


167  Array.Copy(v.arr, arr, arr.Length);


168  return new Vector(arr);


169  }


170  }


171  }

