Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/13/19 17:29:49 (5 years ago)
Author:
gkronber
Message:

#2925: efficiency improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Vector.cs

    r16601 r16604  
    1717    }
    1818
    19     // public static Vector AddTo(Vector a, Vector b) {
    20     //   if (b == Zero) return a;
    21     //   if (a == Zero) {
    22     //     var vArr = new double[b.Length];
    23     //     b.CopyTo(vArr);
    24     //     return new Vector(vArr);
    25     //   } else {
    26     //     Trace.Assert(a.arr.Length == b.arr.Length);
    27     //     for (int i = 0; i < a.arr.Length; i++)
    28     //       a.arr[i] += b.arr[i];
    29     //     return a;
    30     //   }
    31     // }
     19    public Vector Add(Vector b) {
     20      var a = this;
     21      if (b == Zero) return a;
     22      if (a == Zero) {
     23        var vArr = new double[b.Length];
     24        b.CopyTo(vArr);
     25        return new Vector(vArr);
     26      } else {
     27        Trace.Assert(a.arr.Length == b.arr.Length);
     28        for (int i = 0; i < a.arr.Length; i++)
     29          a.arr[i] += b.arr[i];
     30        return a;
     31      }
     32    }
    3233
    3334
    34     public static Vector Subtract(Vector a, Vector b) {
     35    public Vector Subtract(Vector b) {
     36      var a = this;
    3537      if (b == Zero) return a;
    3638      if (a == Zero) {
     
    3941      }
    4042      Trace.Assert(a.arr.Length == b.arr.Length);
    41       var res = new double[a.arr.Length];
    4243      for (int i = 0; i < a.arr.Length; i++)
    43         res[i] = a.arr[i] - b.arr[i];
    44       return new Vector(res);
     44        a.arr[i] -= b.arr[i];
     45      return a;
    4546    }
    4647
    47     public static Vector operator -(Vector a, Vector b) {
    48       return Subtract(a, b);
    49     }
    50     public static Vector operator -(Vector v) {
    51       return v.Scale(-1.0);
    52     }
     48    // public static Vector operator -(Vector a, Vector b) {
     49    //   return Subtract(a, b);
     50    // }
     51    // public static Vector operator -(Vector v) {
     52    //   return v.Scale(-1.0);
     53    // }
    5354
    54     public static Vector operator *(double s, Vector v) {
    55       return v.Scale(s);
    56     }
     55    //public static Vector operator *(double s, Vector v) {
     56    //  return v.Scale(s);
     57    //}
    5758
    5859    public Vector Scale(double s) {
    5960      if (this == Zero) return Zero;
    6061
    61       var res = new double[arr.Length];
    6262      for (int i = 0; i < arr.Length; i++) {
    63         res[i] = arr[i] * s;
     63        arr[i] *= s;
    6464      }
    65       return new Vector(res);
    66 
     65      return this;
    6766    }
    6867
    69     public static Vector operator *(Vector v, double s) {
    70       return s * v;
    71     }
     68    // public static Vector operator *(Vector v, double s) {
     69    //   return s * v;
     70    // }
    7271
    7372    public static Vector operator *(Vector u, Vector v) {
     
    8988    }
    9089
    91     public static Vector operator /(Vector v, double s) {
    92       return v.Scale(1.0 / s);
     90    // public static Vector operator /(Vector v, double s) {
     91    //   return v.Scale(1.0 / s);
     92    // }
     93
     94    public Vector Sin() {
     95      for (int i = 0; i < arr.Length; i++) arr[i] = Math.Sin(arr[i]);
     96      return this;
    9397    }
    94 
    95     public static Vector Sin(Vector s) {
    96       var res = new double[s.arr.Length];
    97       for (int i = 0; i < res.Length; i++) res[i] = Math.Sin(s.arr[i]);
    98       return new Vector(res);
    99     }
    100     public static Vector Cos(Vector s) {
    101       var res = new double[s.arr.Length];
    102       for (int i = 0; i < res.Length; i++) res[i] = Math.Cos(s.arr[i]);
    103       return new Vector(res);
     98    public Vector Cos() {
     99      for (int i = 0; i < arr.Length; i++) arr[i] = Math.Cos(arr[i]);
     100      return this;
    104101    }
    105102
     
    137134    }
    138135
     136    public void CopyTo(double[,] target, int rowIdx) {
     137      if (target == null) return;
     138      if (this == Zero) {
     139        for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = 0.0;
     140      } else {
     141        for (int j = 0; j < target.GetLength(1); j++) target[rowIdx, j] = arr[j];
     142      }
     143    }
    139144    /// <summary>
    140145    /// Creates a new vector
     
    165170      return new Vector(gArr);
    166171    }
     172
     173    internal static Vector CreateFromMatrixRow(double[,] jac, int rowIdx) {
     174      var arr = new double[jac.GetLength(1)];
     175      for (int i = 0; i < arr.Length; i++) arr[i] = jac[rowIdx, i];
     176      return new Vector(arr);
     177    }
    167178  }
    168179}
Note: See TracChangeset for help on using the changeset viewer.