Skip to content

Commit 45385bb

Browse files
committed
Minor changes to Permutation class.
Add overloads for Invert() and IsValid() methods taking the permutation length as argument.
1 parent cf52d99 commit 45385bb

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

CSparse/CSparse.csproj

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
55
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
6-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
77
<PackageId>CSparse</PackageId>
88
<Summary>A concise library for solving sparse linear systems with direct methods.</Summary>
99
<Description>CSparse.NET provides numerical methods for sparse LU, Cholesky and QR decomposition of real and complex linear systems.</Description>
1010
<Product>CSparse.NET</Product>
1111
<Company />
12-
<Copyright>Copyright Christian Woltering © 2012-2022</Copyright>
12+
<Copyright>Copyright Christian Woltering © 2012-2023</Copyright>
1313
<Authors>Christian Woltering</Authors>
14-
<AssemblyVersion>3.7.0.0</AssemblyVersion>
15-
<FileVersion>3.7.0.0</FileVersion>
14+
<AssemblyVersion>3.8.1.0</AssemblyVersion>
15+
<FileVersion>3.8.1.0</FileVersion>
1616
<PackageTags>math sparse matrix lu cholesky qr decomposition factorization </PackageTags>
17-
<Version>3.7.0</Version>
17+
<Version>3.8.1</Version>
1818
<AssemblyName>CSparse</AssemblyName>
1919
<RootNamespace>CSparse</RootNamespace>
2020
<PackageLicenseExpression>LGPL-2.1-only</PackageLicenseExpression>
2121
<PackageProjectUrl>https://github.com/wo80/CSparse.NET</PackageProjectUrl>
2222
<RepositoryUrl>https://github.com/wo80/CSparse.NET</RepositoryUrl>
2323
<RepositoryType>git</RepositoryType>
24-
<PackageReleaseNotes>Add sparse matrix OfDiagonals static method (similar to MATLAB spdiags).
25-
</PackageReleaseNotes>
24+
<PackageReleaseNotes>
25+
Changes in version 3.8.1:
26+
27+
* Add overloads for permutation Invert() and IsValid() methods taking the permutation length as argument.
28+
29+
Changes in version 3.8.0:
30+
31+
* Add overloads for the factorization Solve() methods taking Span&lt;T&gt; as argument. Note that this introduces a dependency on System.Memory for the netstandard2.0 assembly.
32+
</PackageReleaseNotes>
2633
</PropertyGroup>
2734

2835
<ItemGroup>
29-
<PackageReference Include="System.Memory" Version="4.5.5" Condition="'$(TargetFramework)' == 'netstandard2.0'"/>
36+
<PackageReference Include="System.Memory" Version="4.5.5" Condition="'$(TargetFramework)' == 'netstandard2.0'"/>
3037
</ItemGroup>
3138

3239
<ItemGroup>

CSparse/Permutation.cs

+26-7
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,20 @@ public static int[] Create(int n, int seed = 0)
142142
/// <returns>Returns pinv[i] = k if p[k] = i on input.</returns>
143143
public static int[] Invert(int[] p)
144144
{
145-
int k, n = p.Length;
145+
return Invert(p, p.Length);
146+
}
146147

148+
/// <summary>
149+
/// Inverts a permutation vector.
150+
/// </summary>
151+
/// <param name="p">A permutation vector.</param>
152+
/// <param name="n">Length of the permutation.</param>
153+
/// <returns>Returns pinv[i] = k if p[k] = i on input.</returns>
154+
public static int[] Invert(int[] p, int n)
155+
{
147156
int[] pinv = new int[n];
148157

149-
for (k = 0; k < n; k++)
158+
for (int k = 0; k < n; k++)
150159
{
151160
// Invert the permutation.
152161
pinv[p[k]] = k;
@@ -162,21 +171,31 @@ public static int[] Invert(int[] p)
162171
/// <returns>True if <paramref name="p"/> represents a proper permutation, <c>false</c> otherwise.</returns>
163172
public static bool IsValid(int[] p)
164173
{
165-
int length = p.Length;
174+
return IsValid(p, p.Length);
175+
}
166176

167-
var check = new bool[length];
177+
/// <summary>
178+
/// Checks whether the <paramref name="p"/> array represents a proper permutation.
179+
/// </summary>
180+
/// <param name="p">An array which represents where each integer is permuted too: indices[i]
181+
/// represents that integer i is permuted to location indices[i].</param>
182+
/// <param name="n">Length of the permutation.</param>
183+
/// <returns>True if <paramref name="p"/> represents a proper permutation, <c>false</c> otherwise.</returns>
184+
public static bool IsValid(int[] p, int n)
185+
{
186+
var check = new bool[n];
168187

169-
for (int i = 0; i < length; i++)
188+
for (int i = 0; i < n; i++)
170189
{
171-
if (p[i] >= length || p[i] < 0)
190+
if (p[i] >= n || p[i] < 0)
172191
{
173192
return false;
174193
}
175194

176195
check[p[i]] = true;
177196
}
178197

179-
for (int i = 0; i < length; i++)
198+
for (int i = 0; i < n; i++)
180199
{
181200
if (check[i] == false)
182201
{

0 commit comments

Comments
 (0)