1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
#include "vtkDiagonalMatrixSource.h"
#include "vtkArrayData.h"
#include "vtkDenseArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkSparseArray.h"
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkDiagonalMatrixSource);
//------------------------------------------------------------------------------
vtkDiagonalMatrixSource::vtkDiagonalMatrixSource()
: ArrayType(DENSE)
, Extents(3)
, Diagonal(1.0)
, SuperDiagonal(0.0)
, SubDiagonal(0.0)
, RowLabel(nullptr)
, ColumnLabel(nullptr)
{
this->SetRowLabel("rows");
this->SetColumnLabel("columns");
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
}
//------------------------------------------------------------------------------
vtkDiagonalMatrixSource::~vtkDiagonalMatrixSource()
{
this->SetRowLabel(nullptr);
this->SetColumnLabel(nullptr);
}
//------------------------------------------------------------------------------
void vtkDiagonalMatrixSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ArrayType: " << this->ArrayType << endl;
os << indent << "Extents: " << this->Extents << endl;
os << indent << "Diagonal: " << this->Diagonal << endl;
os << indent << "SuperDiagonal: " << this->SuperDiagonal << endl;
os << indent << "SubDiagonal: " << this->SubDiagonal << endl;
os << indent << "RowLabel: " << (this->RowLabel ? this->RowLabel : "") << endl;
os << indent << "ColumnLabel: " << (this->ColumnLabel ? this->ColumnLabel : "") << endl;
}
//------------------------------------------------------------------------------
int vtkDiagonalMatrixSource::RequestData(
vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
{
if (this->Extents < 1)
{
vtkErrorMacro(<< "Invalid matrix extents: " << this->Extents << "x" << this->Extents
<< " array is not supported.");
return 0;
}
vtkArray* array = nullptr;
switch (this->ArrayType)
{
case DENSE:
array = this->GenerateDenseArray();
break;
case SPARSE:
array = this->GenerateSparseArray();
break;
default:
vtkErrorMacro(<< "Invalid array type: " << this->ArrayType << ".");
return 0;
}
vtkArrayData* const output = vtkArrayData::GetData(outputVector);
output->ClearArrays();
output->AddArray(array);
array->Delete();
return 1;
}
vtkArray* vtkDiagonalMatrixSource::GenerateDenseArray()
{
vtkDenseArray<double>* const array = vtkDenseArray<double>::New();
array->Resize(vtkArrayExtents::Uniform(2, this->Extents));
array->SetDimensionLabel(0, this->RowLabel);
array->SetDimensionLabel(1, this->ColumnLabel);
array->Fill(0.0);
if (this->Diagonal != 0.0)
{
for (vtkIdType i = 0; i != this->Extents; ++i)
array->SetValue(vtkArrayCoordinates(i, i), this->Diagonal);
}
if (this->SuperDiagonal != 0.0)
{
for (vtkIdType i = 0; i + 1 != this->Extents; ++i)
array->SetValue(vtkArrayCoordinates(i, i + 1), this->SuperDiagonal);
}
if (this->SubDiagonal != 0.0)
{
for (vtkIdType i = 0; i + 1 != this->Extents; ++i)
array->SetValue(vtkArrayCoordinates(i + 1, i), this->SubDiagonal);
}
return array;
}
vtkArray* vtkDiagonalMatrixSource::GenerateSparseArray()
{
vtkSparseArray<double>* const array = vtkSparseArray<double>::New();
array->Resize(vtkArrayExtents::Uniform(2, this->Extents));
array->SetDimensionLabel(0, this->RowLabel);
array->SetDimensionLabel(1, this->ColumnLabel);
if (this->Diagonal != 0.0)
{
for (vtkIdType i = 0; i != this->Extents; ++i)
array->AddValue(vtkArrayCoordinates(i, i), this->Diagonal);
}
if (this->SuperDiagonal != 0.0)
{
for (vtkIdType i = 0; i + 1 != this->Extents; ++i)
array->AddValue(vtkArrayCoordinates(i, i + 1), this->SuperDiagonal);
}
if (this->SubDiagonal != 0.0)
{
for (vtkIdType i = 0; i + 1 != this->Extents; ++i)
array->AddValue(vtkArrayCoordinates(i + 1, i), this->SubDiagonal);
}
return array;
}
VTK_ABI_NAMESPACE_END
|