1  using System;


2  using System.Collections.Generic;


3  using System.Diagnostics;


4  using System.Linq;


5  using System.Text;


6  using System.Threading.Tasks;


7 


8  namespace HeuristicLab.Problems.DynamicalSystemsModelling {


9  public class Vector {


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


11 


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


13  if (a == Zero) return b;


14  if (b == Zero) return a;


15  Debug.Assert(a.arr.Length == b.arr.Length);


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


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


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


19  return new Vector(res);


20  }


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


22  if (b == Zero) return a;


23  if (a == Zero) return b;


24  Debug.Assert(a.arr.Length == b.arr.Length);


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


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


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


28  return new Vector(res);


29  }


30  public static Vector operator (Vector v) {


31  if (v == Zero) return Zero;


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


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


34  return v;


35  }


36 


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


38  if (v == Zero) return Zero;


39  if (s == 0.0) return Zero;


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


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


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


43  return new Vector(res);


44  }


45 


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


47  return s * v;


48  }


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


50  if (v == Zero) return Zero;


51  if (u == Zero) return Zero;


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


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


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


55  return new Vector(res);


56  }


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


58  if (s == 0.0) return Zero;


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


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


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


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


63  return new Vector(res);


64  }


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


66  return v * 1.0 / s;


67  }


68 


69  public static Vector Sin(Vector s) {


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


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


72  return new Vector(res);


73  }


74  public static Vector Cos(Vector s) {


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


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


77  return new Vector(res);


78  }


79 


80  public double this[int idx] {


81  get {


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


83  else return arr[idx];


84  }


85  set {


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


87  else arr[idx] = value;


88  }


89  }


90 


91  public int Length {


92  get {


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


94  else return arr.Length;


95  }


96  }


97 


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


99 


100  public Vector(double[] v) {


101  this.arr = v;


102  }


103 


104  public void CopyTo(double[] target) {


105  Debug.Assert(arr.Length <= target.Length);


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


107  }


108  }


109  }

