#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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.Serialization;
using Google.Protobuf;
using HEAL.Attic;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Storage;
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("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 { }
[Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
public class DoubleVectorTransformer : VectorTransformer { }
#endregion
#region Dense Vector Storage Transformer
[Transformer("CA28BA16-2C95-4270-9A04-3502D492F070", 200)]
public class DenseDoubleVectorStorageTransformer : BoxTransformer> {
public override Box CreateBox(object o, Mapper mapper) {
var box = base.CreateBox(o, mapper);
box.Values = new RepeatedValueBox() {
Doubles = new RepeatedDoubleBox()
};
return box;
}
protected override void Populate(Box box, DenseVectorStorage storage, Mapper mapper) {
box.Values.Doubles.Values.AddRange(storage.Data);
}
protected override DenseVectorStorage Extract(Box box, Type type, Mapper mapper) {
var data = box.Values.Doubles.Values;
return DenseVectorStorage.OfEnumerable(data);
}
}
#endregion
#region Sparse Vector Storage Transformer
[Transformer("25FDFB3B-5849-44F6-81AC-3D16CB093ADE", 200)]
public class SparseDoubleVectorStorageTransformer : BoxTransformer> {
public override Box CreateBox(object o, Mapper mapper) {
var box = base.CreateBox(o, mapper);
box.Value = new ScalarValueBox();
box.Values = new RepeatedValueBox() {
Doubles = new RepeatedDoubleBox(),
Ints = new RepeatedIntBox()
};
return box;
}
protected override void Populate(Box box, SparseVectorStorage storage, Mapper mapper) {
box.Value.Long = storage.ValueCount;
box.Values.Doubles.Values.AddRange(storage.Values);
box.Values.Ints.Values.AddRange(storage.Indices);
}
protected override SparseVectorStorage Extract(Box box, Type type, Mapper mapper) {
int valueCount = (int)box.Value.Long;
var values = box.Values.Doubles.Values;
var indices = box.Values.Ints.Values;
var data = indices.Zip(values, Tuple.Create);
return SparseVectorStorage.OfIndexedEnumerable(valueCount, data);
}
}
#endregion
[Transformer("45EFC879-241D-404F-BCC1-341A41F6AB90", 1000)]
public class DataContractTransformer : BoxTransformer