Skip to content

Commit af8b3a2

Browse files
committed
add spmatrix class. #190
1 parent ec5770f commit af8b3a2

File tree

12 files changed

+258
-5
lines changed

12 files changed

+258
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
/// <summary>
9+
/// base matrix class for compressed row and column oriented matrices
10+
/// </summary>
11+
public partial class _cs_matrix
12+
{
13+
public NDArray data { get; set; }
14+
15+
public int ndim => data.ndim;
16+
17+
public Type dtype => data.dtype;
18+
19+
public Shape shape => (Shape)data.Storage.Shape;
20+
21+
public int maxprint { get; set; }
22+
23+
public int nnz
24+
{
25+
get
26+
{
27+
switch (data.dtype.Name)
28+
{
29+
case "Int32":
30+
return data.Data<int>().Length;
31+
case "Double":
32+
return data.Data<double>().Length;
33+
}
34+
35+
return 0;
36+
}
37+
}
38+
39+
public bool has_canonical_format { get; set; }
40+
41+
public bool has_sorted_indices { get; set; }
42+
43+
public string format { get; set; }
44+
45+
public NDArray indices { get; set; }
46+
47+
public NDArray indptr { get; set; }
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
public partial class _cs_matrix
9+
{
10+
public static matrix operator *(_cs_matrix m1, matrix m2)
11+
{
12+
m1.transpose();
13+
var (M, N) = m1.shape.BiShape;
14+
15+
var result = np.zeros(M);
16+
17+
spmatrix.csc_matvec(M, N, m1.indptr.Data<int>(), m1.indices.Data<int>(), m1.data.Data<double>(), m2.Data<double>(), result);
18+
19+
m1.transpose();
20+
return np.asmatrix(np.asmatrix(result).transpose());
21+
}
22+
23+
public static matrix operator *(matrix m2, _cs_matrix m1)
24+
{
25+
return m1 * m2;
26+
}
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
public partial class _cs_matrix
9+
{
10+
/// <summary>
11+
/// Sum the matrix over the given axis. If the axis is None, sum over both rows and columns, returning a scalar.
12+
/// </summary>
13+
/// <param name="axis"></param>
14+
/// <returns></returns>
15+
public matrix sum(int? axis = null)
16+
{
17+
return spmatrix.sum(this, axis: 0, dtype: dtype);
18+
}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace NumSharp.Core.Sparse
8+
{
9+
public partial class _cs_matrix
10+
{
11+
public _cs_matrix transpose()
12+
{
13+
if (data.ndim == 1)
14+
{
15+
data = data.transpose();
16+
}
17+
else
18+
{
19+
data.reshape(data.shape.Reverse().ToArray());
20+
}
21+
22+
return this;
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
public partial class csr_matrix : _cs_matrix
9+
{
10+
public csr_matrix(NDArray data, NDArray indices, NDArray indptr, Shape shape = null, Type dtype = null)
11+
{
12+
maxprint = 50;
13+
format = "csf";
14+
15+
this.data = data;
16+
this.indices = indices;
17+
this.indptr = indptr;
18+
19+
if(shape != null)
20+
{
21+
data.reshape(shape);
22+
}
23+
}
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NumSharp.Core.Sparse
6+
{
7+
public partial class csr_matrix
8+
{
9+
public void sort_indices()
10+
{
11+
int indptrstart = 0;
12+
int indptrend = 0;
13+
int tmpindicesvalue = 0;
14+
double tmpdatavalue = 0;
15+
16+
for (int i = 1; i < indptr.size; i++)
17+
{
18+
indptrend = indptr.Data<int>()[i] - 1;
19+
20+
for (int j = indptrstart; j < indptrend; j++)
21+
{
22+
23+
if (indices.Data<int>()[j + 1] < indices.Data<int>()[j])
24+
{
25+
// switch indices
26+
tmpindicesvalue = indices.Data<int>()[j];
27+
indices.Data<int>()[j] = indices.Data<int>()[j + 1];
28+
indices.Data<int>()[j + 1] = tmpindicesvalue;
29+
30+
// switch value
31+
tmpdatavalue = data.Data<double>()[j];
32+
data.Data<double>()[j] = data.Data<double>()[j + 1];
33+
data.Data<double>()[j + 1] = tmpdatavalue;
34+
}
35+
}
36+
37+
indptrstart = indptr.Data<int>()[i];
38+
}
39+
40+
has_sorted_indices = true;
41+
}
42+
}
43+
}

src/NumSharp.Core/Sparse/sparse.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
public class sparse
9+
{
10+
public csr_matrix diags(NDArray[] diagonals, int[] offsets = null, Shape shape = null, string format = null, Type dtype = null)
11+
{
12+
var (m, n) = shape.BiShape;
13+
var data_arr = np.zeros(m);
14+
15+
for (int j = 0; j < diagonals.Length; j++)
16+
{
17+
var diagonal = diagonals[j];
18+
}
19+
20+
return null;
21+
}
22+
}
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core.Sparse
7+
{
8+
public class spmatrix
9+
{
10+
public static matrix sum(_cs_matrix mx, int? axis = null, Type dtype = null)
11+
{
12+
var (m, n) = mx.shape.BiShape;
13+
matrix ret = null;
14+
15+
// sum over columns
16+
if (axis == 0)
17+
{
18+
var matrix = np.asmatrix(np.ones(new Shape(1, m), dtype));
19+
ret = matrix * mx;
20+
}
21+
22+
return ret;
23+
}
24+
25+
public static void csc_matvec(int n_row, int n_col, int[] Ap, int[] Ai, double[] Ax, double[] Xx, NDArray Yx)
26+
{
27+
for (int j = 0; j < n_col; j++)
28+
{
29+
int col_start = Ap[j];
30+
int col_end = Ap[j + 1];
31+
32+
for (int ii = col_start; ii < col_end; ii++)
33+
{
34+
int i = Ai[ii];
35+
Yx.Data<double>()[i] += Ax[ii] * Xx[j];
36+
}
37+
}
38+
}
39+
}
40+
}

src/NumSharp.PowerShell/NumSharp.PowerShell.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.1">
10+
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.2">
1111
<PrivateAssets>all</PrivateAssets>
1212
</PackageReference>
13-
<PackageReference Include="NumSharp" Version="0.6.4" />
13+
<PackageReference Include="NumSharp" Version="0.6.5" />
1414
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
1515
</ItemGroup>
1616

src/NumSharp.Python/NumSharp.Python.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<ItemGroup>
4-
<PackageReference Include="NumSharp" Version="0.6.4" />
4+
<PackageReference Include="NumSharp" Version="0.6.5" />
55
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
66
</ItemGroup>
77

0 commit comments

Comments
 (0)