Skip to content

Commit a5b1873

Browse files
committed
增加类似一次python当中矩阵-1的选取方法
1 parent bc8d28d commit a5b1873

3 files changed

Lines changed: 120 additions & 13 deletions

File tree

src/Numerics/LinearAlgebra/Int/DenseMatrix.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int
149149
/// This new matrix will be independent from the enumerable.
150150
/// A new memory block will be allocated for storing the matrix.
151151
/// </summary>
152-
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<(int, int,int)> enumerable)
152+
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<(int, int, int)> enumerable)
153153
{
154154
return new DenseMatrix(DenseColumnMajorMatrixStorage<int>.OfIndexedEnumerable(rows, columns, enumerable));
155155
}
@@ -564,7 +564,7 @@ protected override void DoMultiply(int scalar, Matrix<int> result)
564564
//}
565565
//else
566566
//{
567-
base.DoMultiply(scalar, result);
567+
base.DoMultiply(scalar, result);
568568
//}
569569
}
570570

@@ -589,7 +589,7 @@ protected override void DoMultiply(Vector<int> rightSide, Vector<int> result)
589589
//}
590590
//else
591591
//{
592-
base.DoMultiply(rightSide, result);
592+
base.DoMultiply(rightSide, result);
593593
//}
594594
}
595595

@@ -710,7 +710,7 @@ protected override void DoTransposeThisAndMultiply(Vector<int> rightSide, Vector
710710
//}
711711
//else
712712
//{
713-
base.DoTransposeThisAndMultiply(rightSide, result);
713+
base.DoTransposeThisAndMultiply(rightSide, result);
714714
//}
715715
}
716716

@@ -777,7 +777,7 @@ protected override void DoDivide(int divisor, Matrix<int> result)
777777
//}
778778
//else
779779
//{
780-
base.DoDivide(divisor, result);
780+
base.DoDivide(divisor, result);
781781
//}
782782
}
783783

@@ -795,7 +795,7 @@ protected override void DoPointwiseMultiply(Matrix<int> other, Matrix<int> resul
795795
//}
796796
//else
797797
//{
798-
base.DoPointwiseMultiply(other, result);
798+
base.DoPointwiseMultiply(other, result);
799799
//}
800800
}
801801

@@ -813,7 +813,7 @@ protected override void DoPointwiseDivide(Matrix<int> divisor, Matrix<int> resul
813813
//}
814814
//else
815815
//{
816-
base.DoPointwiseDivide(divisor, result);
816+
base.DoPointwiseDivide(divisor, result);
817817
//}
818818
}
819819

@@ -831,7 +831,7 @@ protected override void DoPointwisePower(Matrix<int> exponent, Matrix<int> resul
831831
//}
832832
//else
833833
//{
834-
base.DoPointwisePower(exponent, result);
834+
base.DoPointwisePower(exponent, result);
835835
//}
836836
}
837837

@@ -1192,7 +1192,7 @@ public override bool IsSymmetric()
11921192
var index = j * RowCount;
11931193
for (var i = j + 1; i < RowCount; i++)
11941194
{
1195-
if (_values[(i*ColumnCount) + j] != _values[index + i])
1195+
if (_values[(i * ColumnCount) + j] != _values[index + i])
11961196
{
11971197
return false;
11981198
}

src/Numerics/LinearAlgebra/Matrix.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ protected Matrix(MatrixStorage<T> storage)
116116
{
117117
throw new Exception("colRep should be \":\" constantly.");
118118
}
119-
return Row(iRow);
119+
if(iRow!=-1)
120+
{
121+
return Row(iRow);
122+
}
123+
else
124+
{
125+
return Row(RowCount-1);
126+
}
127+
120128
}
121129
[MethodImpl(MethodImplOptions.AggressiveInlining)]
122130
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
@@ -126,7 +134,14 @@ protected Matrix(MatrixStorage<T> storage)
126134
{
127135
throw new Exception("colRep should be \":\" constantly.");
128136
}
129-
SetRow(iRow, value);
137+
if (iRow != -1)
138+
{
139+
SetRow(iRow, value);
140+
}
141+
else
142+
{
143+
SetRow(RowCount - 1, value);
144+
}
130145
}
131146

132147
}
@@ -148,7 +163,14 @@ protected Matrix(MatrixStorage<T> storage)
148163
{
149164
throw new Exception("rowRep should be \":\" constantly.");
150165
}
151-
return Column(iCol);
166+
if (iCol != -1)
167+
{
168+
return Column(iCol);
169+
}
170+
else
171+
{
172+
return Column(ColumnCount - 1);
173+
}
152174
}
153175
[MethodImpl(MethodImplOptions.AggressiveInlining)]
154176
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
@@ -158,7 +180,15 @@ protected Matrix(MatrixStorage<T> storage)
158180
{
159181
throw new Exception("rowRep should be \":\" constantly.");
160182
}
161-
SetColumn(iCol, value);
183+
if (iCol != -1)
184+
{
185+
SetColumn(iCol, value);
186+
}
187+
else
188+
{
189+
SetColumn(ColumnCount - 1, value);
190+
}
191+
162192
}
163193
}
164194

@@ -487,6 +517,37 @@ public Matrix<T> this[IEnumerable<int> rowRep, IEnumerable<int> colRep]
487517
}
488518

489519

520+
public Matrix<T> this[char rowRep,char colRep]
521+
{
522+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
523+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
524+
get
525+
{
526+
if (rowRep != ':')
527+
{
528+
throw new Exception("rowRep should be \":\" constantly.");
529+
}
530+
if (colRep != ':')
531+
{
532+
throw new Exception("colRep should be \":\" constantly.");
533+
}
534+
return Clone();
535+
}
536+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
537+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
538+
set
539+
{
540+
if (rowRep != ':')
541+
{
542+
throw new Exception("rowRep should be \":\" constantly.");
543+
}
544+
if (colRep != ':')
545+
{
546+
throw new Exception("colRep should be \":\" constantly.");
547+
}
548+
SetSubMatrix(0, RowCount, 0, ColumnCount, value);
549+
}
550+
}
490551

491552

492553

src/Numerics/LinearAlgebra/Vector.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
using System.Collections;
3333
using System.Collections.Generic;
3434
using System.Diagnostics;
35+
using System.Linq;
3536
using System.Runtime;
3637
using System.Runtime.CompilerServices;
3738

@@ -122,10 +123,55 @@ public T this[int index]
122123
}
123124

124125

126+
public Vector<T> this[char str]
127+
{
128+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
130+
get
131+
{
132+
if (str != ':')
133+
{
134+
throw new Exception("rowRep should be \":\" constantly.");
135+
}
136+
return this.Clone();
137+
}
138+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
139+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
140+
set
141+
{
142+
SetSubVector(0, this.Count, value);
143+
}
144+
}
125145

146+
public Vector<T> this[IEnumerable<int> rowRep]
147+
{
148+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
150+
get
151+
{
152+
int roenum = rowRep.Count();
153+
Vector<T> res= Vector<T>.Build.Dense( roenum);
154+
for (int i = 0; i < roenum; i++)
155+
{
156+
res[i] = this[rowRep.ElementAt(i)];
157+
}
158+
return res;
126159

160+
}
161+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162+
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
163+
set
164+
{
165+
int roenum = rowRep.Count();
166+
for (int i = 0; i < roenum; i++)
167+
{
168+
this[rowRep.ElementAt(i)] = value[i];
169+
}
127170

128171

172+
}
173+
}
174+
129175

130176

131177

0 commit comments

Comments
 (0)