Commit c608c13a authored by Timothy M. Shead's avatar Timothy M. Shead

Added support for general p-norms to vtkNormalizeMatrixVectors

Previously, vtkNormalizeMatrixVectors only supported 2-norms,
which are still the default.
parent 70e08d6b
......@@ -34,7 +34,8 @@
vtkStandardNewMacro(vtkNormalizeMatrixVectors);
vtkNormalizeMatrixVectors::vtkNormalizeMatrixVectors() :
VectorDimension(1)
VectorDimension(1),
PValue(2)
{
}
......@@ -46,6 +47,7 @@ void vtkNormalizeMatrixVectors::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "VectorDimension: " << this->VectorDimension << endl;
os << indent << "PValue: " << this->PValue << endl;
}
int vtkNormalizeMatrixVectors::RequestData(
......@@ -54,6 +56,7 @@ int vtkNormalizeMatrixVectors::RequestData(
vtkInformationVector* outputVector)
{
int vector_dimension = vtkstd::min(1, vtkstd::max(0, this->VectorDimension));
double p_value = vtkstd::max(1.0, this->PValue);
vtkArrayData* const input = vtkArrayData::GetData(inputVector[0]);
if(input->GetNumberOfArrays() != 1)
......@@ -88,13 +91,13 @@ int vtkNormalizeMatrixVectors::RequestData(
for(vtkIdType n = 0; n != value_count; ++n)
{
output_array->GetCoordinatesN(n, coordinates);
weight[coordinates[vector_dimension] - vectors.GetBegin()] += pow(output_array->GetValueN(n), 2);
weight[coordinates[vector_dimension] - vectors.GetBegin()] += pow(output_array->GetValueN(n), p_value);
}
// Convert the sums into weights, avoiding divide-by-zero ...
for(vtkIdType i = 0; i != vectors.GetSize(); ++i)
{
const double length = sqrt(weight[i]);
const double length = pow(weight[i],1.0/p_value);
weight[i] = length ? 1.0 / length : 0.0;
}
......
......@@ -20,7 +20,8 @@
=========================================================================*/
// .NAME vtkNormalizeMatrixVectors - given a sparse input matrix, produces
// a sparse output matrix with each vector normalized to unit length.
// a sparse output matrix with each vector normalized to unit length with respect
// to a p-norm (default p=2).
//
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
......@@ -42,6 +43,11 @@ public:
vtkGetMacro(VectorDimension, int);
vtkSetMacro(VectorDimension, int);
// Description:
// Value of p in p-norm normalization, subject to p >= 1. Default is p=2 (Euclidean norm).
vtkGetMacro(PValue, double);
vtkSetMacro(PValue, double);
protected:
vtkNormalizeMatrixVectors();
~vtkNormalizeMatrixVectors();
......@@ -52,6 +58,7 @@ protected:
vtkInformationVector*);
int VectorDimension;
double PValue;
private:
vtkNormalizeMatrixVectors(const vtkNormalizeMatrixVectors&); // Not implemented
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment