Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/19 16:31:11 (4 years ago)
Author:
pfleck
Message:

#3040

  • Added VectorVariable and its corresponding TreeNode.
  • Added symbols for basic vector operations and symbols for sum and avg of vectors.
  • Added a VectorEvaluate method in the SymbolicDataAnalysisExpressionTreeInterpreter that returns a DoubleVector instead of a double that is also called within the regular Evaluate method.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Vectors/DoubleVector.cs

    r17365 r17367  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
     24using System.Linq;
     25using System.Windows.Markup;
    2326using HEAL.Attic;
    2427
     
    3538      : base(_) { }
    3639
     40    public static DoubleVector operator +(DoubleVector lhs, DoubleVector rhs) {
     41      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l + r));
     42    }
     43    public static DoubleVector operator -(DoubleVector lhs, DoubleVector rhs) {
     44      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l - r));
     45    }
     46    public static DoubleVector operator *(DoubleVector lhs, DoubleVector rhs) {
     47      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l * r));
     48    }
     49    public static DoubleVector operator /(DoubleVector lhs, DoubleVector rhs) {
     50      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l / r));
     51    }
     52    private static IEnumerable<double> VecCalc(IReadOnlyList<double> lhs, IReadOnlyList<double> rhs, Func<double, double, double> resultFunc) {
     53      if (lhs.Count != rhs.Count) throw new InvalidOperationException("Vectors are not of equal length.");
     54      return lhs.Zip(rhs, resultFunc);
     55    }
     56
     57    public static DoubleVector operator +(DoubleVector lhs, double rhs) {
     58      return new DoubleVector(lhs.Select(l => l + rhs));
     59    }
     60    public static DoubleVector operator -(DoubleVector lhs, double rhs) {
     61      return new DoubleVector(lhs.Select(l => l - rhs));
     62    }
     63    public static DoubleVector operator *(DoubleVector lhs, double rhs) {
     64      return new DoubleVector(lhs.Select(l => l * rhs));
     65    }
     66    public static DoubleVector operator /(DoubleVector lhs, double rhs) {
     67      return new DoubleVector(lhs.Select(l => l / rhs));
     68    }
     69
     70    public double Sum() {
     71      return values.Sum();
     72    }
     73
     74    public double Average() {
     75      return values.Average();
     76    }
    3777  }
    3878}
Note: See TracChangeset for help on using the changeset viewer.