本文整理汇总了C#中Vector.Subtract方法的典型用法代码示例。如果您正苦于以下问题:C# Vector.Subtract方法的具体用法?C# Vector.Subtract怎么用?C# Vector.Subtract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector
的用法示例。
在下文中一共展示了Vector.Subtract方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Subtract
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (this.Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
Subtract(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.Subtract(other);
}
}
示例2: GetWeights
public static Vector<double> GetWeights(Matrix<double> data, Vector<double> targetClassification)
{
var features = data.ColumnCount;
// these are things we are trying to solve for
Vector<double> weights = DenseVector.Create(features, i => 1.0);
var alpha = 0.001;
foreach (var cycle in Enumerable.Range(0, 500))
{
#region Sigmoid Explanation
/*
* multiply all the data by the weights, this gives you the estimation of the current function
* given the weights. multiplying by the sigmoid moves a point into one class vs the other
* if its larger than 0.5 it'll be in class 1, if its smaller than it'll be in class 0. The closer it is
* to 1 means that with the given weights that value is highly probably to be in class 1.
*
* it doesn't matter if the instance is the class the sigmoid says it is,
* the error will shift the weights gradient so over the iterations of the cycles
* the weights will move the final data point towards its actual expected class
*
* for example, if there is a data point with values
*
* [1.0, -0.017612, 14.053064]
*
* where value 1 is the initial weight factor, and values 2 and 3 are the x y coordinates
*
* and lets say the point is categorized at class 0.
*
* Calculating the initial sigma gives you something like 0.9999998
*
* which says its class 1, but thats obviously wrong. However, the error rate here is large
*
* meaning that the gradient wants to move towards the expected data.
*
* As you run the ascent this data point will get smaller and smaller and eventually
*
* the sigmoid will classify it properly
*/
#endregion
var currentData = DenseVector.OfEnumerable(data.Multiply(weights).Select(Sigmoid));
#region Error Explanation
// find out how far off we are from the actual expectation. this is
// like the x2 - x1 part of a derivative
#endregion
var error = targetClassification.Subtract(currentData);
#region Gradient Explanation
// this gives you the direction of change from the current
// set of data. At this point every point is moving in the direction
// of the error rate. A large error means we are far off and trying to move
// towards the actual data, a low error rate means we are really close
// to the target data (so the gradient will be smaller, meaning less delta)
#endregion
var gradient = data.Transpose() * error;
#region Weights Update Explanation
// multiplying by alpha means we'll take a small step in the direction
// of the gradient and add it to the current weights. An initial weights of 1.0
// means we're going to start at the current location of where we are.
#endregion
weights = weights + alpha * gradient;
}
return weights;
}
示例3: SubtractRaiseExceptionIfVectorsHaveDifferenteLengths
public void SubtractRaiseExceptionIfVectorsHaveDifferenteLengths()
{
Vector vector1 = new Vector(new double[] { 1.0, 2.0, 3.0 });
Vector vector2 = new Vector(new double[] { 4.0, 5.0, 6.0, 7.0 });
try
{
vector1.Subtract(vector2);
Assert.Fail();
}
catch (Exception ex)
{
Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
Assert.AreEqual("Vectors have different lengths", ex.Message);
}
}
示例4: SimpleSubtract
public void SimpleSubtract()
{
Vector vector1 = new Vector(new double[] { 1.0, 2.0, 3.0 });
Vector vector2 = new Vector(new double[] { 4.0, 5.0, 6.0 });
Vector vector = vector1.Subtract(vector2);
Assert.AreEqual(3, vector.Size);
var elements = vector.Elements;
Assert.AreEqual(1.0 - 4.0, elements[0]);
Assert.AreEqual(2.0 - 5.0, elements[1]);
Assert.AreEqual(3.0 - 6.0, elements[2]);
}
示例5: OBB
//Encontra uma caixa N-dimensional orientada ao eixo de maior significancia
public OBB(List<Vector> nPontos, int nNome, int nNivel)
: base(nPontos, nNome, nNivel)
{
if ((nPontos == null) || (nPontos.Count == 0))
{
return;
}
//Caso exista apenas um ponto não há necessidade de realizar todos os cálculos
if (QntdDados == 1)
{
//o centro é o proprio ponto
this.Centro = this.Pontos[0];
//os autoVectores são os proprios e1, e2, en.
this.AutoVectores = Matrix.Identity(QntdVariaveis, QntdVariaveis);
//necessária para calculos
this.MatrixTransformacao = this.AutoVectores;
//não existe limite para 1 ponto
this.Limites = new Vector(QntdVariaveis);
//apanas para cálculos
this.VectoresFace = Vector.VectorRepetitionLines(Pontos[0], QntdVariaveis);
}
else
{
//Pto médio é diferente de Pto central!!!!!!!!!!!!!!
Vector PontoMedio = MédiaDosPontos(Pontos);
Matrix CovMat = MatrixDeCovariancia(Pontos, PontoMedio);
//EigenvalueDecomposition eig = CovMat.EigenvalueDecomposition;
this.AutoVectores = CovMat.EigenvalueDecomposition.EigenVectors;
//os autoVectores são as colunas
//por isso mudamos para que sejam as linhas
this.AutoVectores.TransposeInplace();
//double[] AutoValores = new double[this.AutoVectores.RowCount];
//for (int i = 0; i < this.AutoVectores.ColumnCount; i++)
//{
// AutoValores[i] = CovMat.EigenvalueDecomposition.EigenValues[i].Real;
//}
// %--------------------------------------------------------
// % Calculo dos eixos
// %--------------------------------------------------------
// %Procura o maior autovalor relativo ao maior autoVector: a direcao dominante da caixa
//double V_Maior = double.NegativeInfinity;
//int i_Maior = 0;
//for (int i = 0; i < Qntd_Variaveis; i++)
//{
// //double temp = AutoValores[i];
// if (AutoValores[i] > V_Maior)
// {
// //Salva a posição da maior direção e seu respectivo valor
// i_Maior = i;
// V_Maior = AutoValores[i];
// }
//}
// o maior autovetor é sempre o ultimo
int i_Maior = AutoVectores.ColumnCount -1;
// %--------------------------------------------------------
// % Calculo dos limites, centro e Vectores <centro-face>
// %--------------------------------------------------------
// % A MatrixTransformacao faz a mudanca do sistema de coordenadas do
// % sistema global para o sistema local da caixa para a qual foi calculada,
// % transformando os Vectores e1, e2, ... , en em av1, av2, ..., avn.
// % Local * AutoVectores = Global
// % Local = Global * Inversa(AutoVectores*) *com os autoVectores nas linhas
// para o caso da Matrix de autoVectores Transposta = Inversa.
this.MatrixTransformacao = this.AutoVectores.Clone();
this.MatrixTransformacao.TransposeInplace();
// % Encontra os limites superiores e inferiores em cada direcao da caixa
Vector Limites_Superiores = new Vector(QntdVariaveis,double.NegativeInfinity);
Vector Limites_Inferiores = new Vector(QntdVariaveis,double.PositiveInfinity);
for (int i = 0; i < QntdDados; i++)
{
//% Muda o sistema de coordenada dos pontos para o sistema local, com o ponto
//% médio como centro. Com base nisto determinamos os limites superiores e
//% inferiores em cada Vector. Obs: Lim_Sup sempre eh + e Lim_Inf sempre eh -.
Vector Valores_Projetado = this.Pontos[i].Subtract(PontoMedio);
Valores_Projetado.MatrixMultiplyInPlace(this.MatrixTransformacao);
Limites_Superiores = Valores_Projetado.Maximum(Limites_Superiores);
Limites_Inferiores = Valores_Projetado.Minimum(Limites_Inferiores);
}
this.Limites = (Limites_Superiores.Subtract(Limites_Inferiores));
this.Limites.ScaleInplace(0.5);
this.Centro = (Limites_Inferiores.Add(Limites_Superiores));
//.........这里部分代码省略.........
示例6: CreateViewMatrix
private Matrix<double> CreateViewMatrix(Vector<double> cameraPosition, Vector<double> cameraTarget, Vector<double> upVector)
{
if (cameraPosition.Count != 3 || cameraTarget.Count != 3 || upVector.Count != 3)
throw new CameraVectorsLengthException();
var zAxis = cameraPosition.Subtract(cameraTarget);
var zAxisNorm = zAxis.Normalize(1);
var xAxis = Cross(upVector, zAxisNorm);
var yAxis = Cross(zAxisNorm, xAxis);
var matrixData = new List<Vector<double>>();
xAxis = xAxis.Normalize(1);
yAxis = yAxis.Normalize(1);
zAxis = zAxis.Normalize(1);
var result = Matrix<double>.Build.DenseOfRowArrays(new List<double[]> {
new double[4] {xAxis[0], yAxis[0], zAxis[0], cameraPosition[0]},
new double[4] {xAxis[1], yAxis[1], zAxis[1], cameraPosition[1]},
new double[4] {xAxis[2], yAxis[2], zAxis[2], cameraPosition[2]},
new double[4] {0,0,0,1} });
return result.Inverse();
}