Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2925 removed unnecessary usings

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