#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HEAL.Attic;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Storage;
using Complex = System.Numerics.Complex;
namespace HeuristicLab.MathNet.Numerics {
#region Vector Transformer
public abstract class VectorTransformer : BoxTransformer> where T : struct, IEquatable, IFormattable {
public override bool CanTransformType(Type type) {
return typeof(Vector).IsAssignableFrom(type);
}
public override Box CreateBox(object o, Mapper mapper) {
var box = base.CreateBox(o, mapper);
box.Value = new ScalarValueBox();
return box;
}
protected override void Populate(Box box, Vector vector, Mapper mapper) {
var storageId = mapper.GetBoxId(vector.Storage);
box.Value.ULong = storageId;
}
protected override Vector Extract(Box box, Type type, Mapper mapper) {
var storageId = (uint)box.Value.ULong;
var storage = (VectorStorage)mapper.GetObject(storageId);
return Vector.Build.OfStorage(storage);
}
}
[Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
public class DoubleVectorTransformer : VectorTransformer { }
[Transformer("78276F91-D075-4472-B81B-3A736184CC22", 400)]
public class SingleVectorTransformer : VectorTransformer { }
[Transformer("9B131989-4528-4A59-B64D-0FE07693714F", 400)]
public class ComplexVectorTransformer : VectorTransformer { }
[Transformer("2D978104-A174-4AE2-AEA1-20FD50CADF0D", 400)]
public class Complex32VectorTransformer : VectorTransformer { }
#endregion
#region Dense Vector Storage Transformer
public abstract class DenseVectorStorageTransformer : BoxTransformer> where T : struct, IEquatable, IFormattable {
public override Box CreateBox(object o, Mapper mapper) {
var box = base.CreateBox(o, mapper);
box.Value = new ScalarValueBox();
return box;
}
protected override void Populate(Box box, DenseVectorStorage storage, Mapper mapper) {
var dataId = mapper.GetBoxId(storage.Data);
box.Value.ULong = dataId;
}
protected override DenseVectorStorage Extract(Box box, Type type, Mapper mapper) {
var dataId = (uint)box.Value.ULong;
var data = (T[])mapper.GetObject(dataId);
return DenseVectorStorage.OfEnumerable(data);
}
public override void FillFromBox(object obj, Box box, Mapper mapper) {
var storage = (DenseVectorStorage)obj;
var dataId = (uint)box.Value.ULong;
var data = (T[])mapper.GetObject(dataId);
Array.Copy(data, storage.Data, data.Length);
}
}
[Transformer("24705F16-FDB8-487A-ABEA-F69D58B50D60", 200)]
public class DoubleDenseVectorStorageTransform : DenseVectorStorageTransformer { }
[Transformer("87FBC62F-E8A0-4C50-9289-CAD2B7F448E4", 200)]
public class SingleDenseVectorStorageTransform : DenseVectorStorageTransformer { }
[Transformer("4391F230-2C0C-4876-B2EF-DE43197F4557", 200)]
public class ComplexDenseVectorStorageTransform : DenseVectorStorageTransformer { }
[Transformer("6E07F8A9-DF19-4A2F-B7A3-209917B58355", 200)]
public class Complex32DenseVectorStorageTransform : DenseVectorStorageTransformer { }
#endregion
#region Sparse Vector Storage Transformer
public abstract class SparseVectorStorageTransformer : BoxTransformer> where T : struct, IEquatable, IFormattable {
public override Box CreateBox(object o, Mapper mapper) {
var box = base.CreateBox(o, mapper);
box.Values = new RepeatedValueBox() {
UInts = new RepeatedUIntBox()
};
return box;
}
protected override void Populate(Box box, SparseVectorStorage storage, Mapper mapper) {
var valueCount = storage.ValueCount;
var valuesId = mapper.GetBoxId(storage.Values);
var indicesId = mapper.GetBoxId(storage.Indices);
box.Values.UInts.Values.AddRange(new[] { (uint)valueCount, valuesId, indicesId });
}
protected override SparseVectorStorage Extract(Box box, Type type, Mapper mapper) {
var boxValues = box.Values.UInts.Values;
int valueCount = (int)boxValues[0];
uint valuesId = boxValues[1];
uint indicesId = boxValues[2];
var values = (T[])mapper.GetObject(valuesId);
var indices = (int[])mapper.GetObject(indicesId);
var data = indices.Zip(values, Tuple.Create);
return SparseVectorStorage.OfIndexedEnumerable(valueCount, data);
}
}
[Transformer("AC9ACF75-7E50-460F-B81A-57D7269548AE", 200)]
public class DoubleSparseVectorStorageTransform : SparseVectorStorageTransformer { }
[Transformer("28BD551A-ACF1-48D0-B83F-C328902C8AB1", 200)]
public class SingleSparseVectorStorageTransform : SparseVectorStorageTransformer { }
[Transformer("B3CF9306-8359-4DEF-B68C-83F1D936A86F", 200)]
public class ComplexSparseVectorStorageTransform : SparseVectorStorageTransformer { }
[Transformer("57674C51-02BC-44D2-81C5-D8F5F6322C87", 200)]
public class Complex32SparseVectorStorageTransform : SparseVectorStorageTransformer { }
#endregion
}