Skip to content

Commit 4feed45

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 59cd545 + d85c5d8 commit 4feed45

24 files changed

+2899
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
*.swp
77
*.vcxproj*
88
*DS_Store
9+
src/Documentation/*/*/*.aux
10+
src/Documentation/*/*/*.blg
11+
src/Documentation/*/*/*.bbl
12+
src/Documentation/*/*/*.log
13+
src/Documentation/*/*/*.out
14+
src/Documentation/*/*/*.toc
915
bin*/*/*
1016
bin64/*
1117
doc/*
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2009 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#include <Core/Algorithms/Math/SortMatrixAlgo.h>
30+
#include <Core/Datatypes/MatrixTypeConversions.h>
31+
#include <Core/Math/MiscMath.h>
32+
33+
using namespace SCIRun;
34+
using namespace SCIRun::Core::Datatypes;
35+
using namespace SCIRun::Core::Algorithms;
36+
using namespace SCIRun::Core::Algorithms::Math;
37+
38+
SortMatrixAlgo::SortMatrixAlgo()
39+
{
40+
//set parameter defaults for UI
41+
addParameter(Variables::Method, 0);
42+
}
43+
44+
45+
AlgorithmOutput SortMatrixAlgo::run_generic(const AlgorithmInput& input) const
46+
{
47+
auto input_matrix = input.get<Matrix>(Variables::InputMatrix);
48+
AlgorithmOutput output;
49+
50+
//sparse support not fully implemented yet.
51+
if (!matrix_is::dense(input_matrix))
52+
{
53+
//TODO implement something with sparse
54+
error("SortMatrix: Currently only works with dense matrices");
55+
output[Variables::OutputMatrix] = 0;
56+
return output;
57+
}
58+
auto mat = matrix_cast::as_dense (input_matrix);
59+
DenseMatrixHandle return_matrix;
60+
61+
//pull parameter from UI
62+
auto method = get(Variables::Method).toInt();
63+
64+
Sort(mat,return_matrix,method);
65+
output[Variables::OutputMatrix] = return_matrix;
66+
return output;
67+
}
68+
69+
70+
bool
71+
SortMatrixAlgo::Sort(DenseMatrixHandle input, DenseMatrixHandle& output,int method) const
72+
{
73+
if (!input)
74+
{
75+
error("SortAscending: no input matrix found");
76+
return false;
77+
}
78+
//get size of original matrix
79+
size_type nrows = input->nrows();
80+
size_type ncols = input->ncols();
81+
//copy original matrix for processing
82+
output.reset(new DenseMatrix(*input));
83+
//pointer to matrix data
84+
double *data = output->data();
85+
86+
if (!output)
87+
{
88+
error("ApplyRowOperation: could not create output matrix");
89+
return false;
90+
}
91+
92+
size_type n = nrows*ncols;
93+
//call the sorting functions
94+
Quicksort(data,0,n-1);
95+
96+
if (method==1)
97+
{
98+
//if set to descending, reverse the order.
99+
output.reset(new DenseMatrix(output -> reverse()));
100+
}
101+
return true;
102+
}
103+
104+
bool
105+
SortMatrixAlgo::Quicksort(double* input, index_type lo, index_type hi) const
106+
{
107+
//splits matrix based on Partition function
108+
index_type ind;
109+
if (lo<hi)
110+
{
111+
ind=Partition(input,lo,hi);
112+
Quicksort(input,lo,ind-1);
113+
Quicksort(input,ind+1,hi);
114+
}
115+
return true;
116+
}
117+
118+
index_type
119+
SortMatrixAlgo::Partition(double* input, index_type lo, index_type hi) const
120+
{
121+
// places the last entry in its proper place in relation to the other
122+
// entries, ie, smaller values before and larger values after.
123+
index_type ind=lo;
124+
125+
double pivot = input[hi];
126+
double tmp;
127+
for (index_type k=lo;k<hi;k++)
128+
{
129+
if (input[k]<=pivot)
130+
{
131+
tmp=input[ind];
132+
input[ind]=input[k];
133+
input[k]=tmp;
134+
ind+=1;
135+
}
136+
}
137+
tmp=input[ind];
138+
input[ind]=input[hi];
139+
input[hi]=tmp;
140+
return ind;
141+
}
142+
143+
144+
145+
146+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2009 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#ifndef CORE_ALGORITHMS_MATH_SortMatrixALGO_H
30+
#define CORE_ALGORITHMS_MATH_SortMatrixALGO_H
31+
32+
#include <Core/Datatypes/Matrix.h>
33+
#include <Core/Datatypes/DenseMatrix.h>
34+
#include <Core/Datatypes/DenseColumnMatrix.h>
35+
36+
37+
#include <string>
38+
#include <sstream>
39+
#include <vector>
40+
#include <algorithm>
41+
42+
#include <Core/Algorithms/Base/AlgorithmVariableNames.h>
43+
#include <Core/Algorithms/Base/AlgorithmBase.h>
44+
#include <Core/Algorithms/Math/share.h>
45+
46+
namespace SCIRun {
47+
namespace Core {
48+
namespace Algorithms {
49+
namespace Math {
50+
51+
class SCISHARE SortMatrixAlgo : public AlgorithmBase
52+
{
53+
54+
public:
55+
SortMatrixAlgo();
56+
AlgorithmOutput run_generic(const AlgorithmInput& input) const;
57+
58+
bool Sort(Datatypes::DenseMatrixHandle input, Datatypes::DenseMatrixHandle& output, int method) const;
59+
60+
bool Quicksort(double* input, index_type lo, index_type hi) const;
61+
index_type Partition(double* input, index_type lo, index_type hi) const;
62+
};
63+
64+
}}}}// end SCIRun namespace
65+
#endif
Binary file not shown.

0 commit comments

Comments
 (0)