Free cookie consent management tool by TermsFeed Policy Generator

# source:branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs@16398

Last change on this file since 16398 was 16398, checked in by gkronber, 5 years ago

#2925: small changes

File size: 3.2 KB
Line
1using System;
2using System.Diagnostics;
3
4namespace 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      Debug.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 operator -(Vector a, Vector b) {
19      if (b == Zero) return a;
20      if (a == Zero) return -b;
21      Debug.Assert(a.arr.Length == b.arr.Length);
22      var res = new double[a.arr.Length];
23      for (int i = 0; i < res.Length; i++)
24        res[i] = a.arr[i] - b.arr[i];
25      return new Vector(res);
26    }
27    public static Vector operator -(Vector v) {
28      if (v == Zero) return Zero;
29      for (int i = 0; i < v.arr.Length; i++)
30        v.arr[i] = -v.arr[i];
31      return v;
32    }
33
34    public static Vector operator *(double s, Vector v) {
35      if (v == Zero) return Zero;
36      if (s == 0.0) return Zero;
37      var res = new double[v.arr.Length];
38      for (int i = 0; i < res.Length; i++)
39        res[i] = s * v.arr[i];
40      return new Vector(res);
41    }
42
43    public static Vector operator *(Vector v, double s) {
44      return s * v;
45    }
46    public static Vector operator *(Vector u, Vector v) {
47      if (v == Zero) return Zero;
48      if (u == Zero) return Zero;
49      var res = new double[v.arr.Length];
50      for (int i = 0; i < res.Length; i++)
51        res[i] = u.arr[i] * v.arr[i];
52      return new Vector(res);
53    }
54    public static Vector operator /(double s, Vector v) {
55      if (s == 0.0) return Zero;
56      if (v == Zero) throw new ArgumentException("Division by zero vector");
57      var res = new double[v.arr.Length];
58      for (int i = 0; i < res.Length; i++)
59        res[i] = 1.0 / v.arr[i];
60      return new Vector(res);
61    }
62    public static Vector operator /(Vector v, double s) {
63      return v * 1.0 / s;
64    }
65
66    public static Vector Sin(Vector s) {
67      var res = new double[s.arr.Length];
68      for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]);
69      return new Vector(res);
70    }
71    public static Vector Cos(Vector s) {
72      var res = new double[s.arr.Length];
73      for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]);
74      return new Vector(res);
75    }
76
77    public double this[int idx] {
78      get {
79        if (this == Vector.Zero) return 0.0;
80        else return arr[idx];
81      }
82      set {
83        if (this == Vector.Zero) throw new InvalidOperationException();
84        else arr[idx] = value;
85      }
86    }
87
88    public int Length {
89      get {
90        if (this == Vector.Zero) throw new InvalidOperationException();
91        else return arr.Length;
92      }
93    }
94
95    private readonly double[] arr; // backing array;
96
97    public Vector(double[] v) {
98      this.arr = v;
99    }
100
101    public void CopyTo(double[] target) {
102      Debug.Assert(arr.Length <= target.Length);
103      Array.Copy(arr, target, arr.Length);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.