@@ -194,35 +194,32 @@ public Entity AsScalar()
194
194
/// <summary>
195
195
/// Finds the symbolical determinant via Laplace's method
196
196
/// </summary>
197
- public Entity Determinant => determinant . GetValue (
198
- static @this => @this . InnerMatrix . DeterminantGaussianSafeDivision ( ) . InnerSimplified ,
197
+ public Entity ? Determinant => determinant . GetValue (
198
+ static @this =>
199
+ {
200
+ if ( ! @this . IsSquare )
201
+ return null ;
202
+ return @this . InnerMatrix . DeterminantGaussianSafeDivision ( ) . InnerSimplified ;
203
+ } ,
199
204
this
200
205
) ;
201
- private FieldCache < Entity > determinant ;
206
+ private FieldCache < Entity ? > determinant ;
202
207
203
- // The reason it's not cached is because it throws exceptions.
204
- /// <summary>Inverts the matrix</summary>
205
- public Matrix ComputeInverse ( )
208
+ /// <summary>Returns an inverse matrix if it exists</summary>
209
+ public Matrix ? Inverse => inverse . GetValue ( static @this =>
206
210
{
207
- var cp = InnerMatrix . Copy ( false ) ;
208
- try
209
- {
210
- cp . InvertMatrix ( ) ;
211
- }
212
- catch ( InvalidShapeException )
213
- {
214
- throw new InvalidMatrixOperationException ( "Cannot inverse a non-square matrix!" ) ;
215
- }
216
- catch ( InvalidDeterminantException )
217
- {
218
- throw new InvalidMatrixOperationException ( "Cannot inverse a singular matrix!" ) ;
219
- }
211
+ var cp = @this . InnerMatrix . Copy ( false ) ;
212
+ if ( ! @this . IsSquare )
213
+ return null ;
214
+ if ( @this . Determinant is null )
215
+ return null ;
216
+ if ( @this . Determinant == 0 )
217
+ return null ;
218
+ cp . InvertMatrix ( ) ;
220
219
return ToMatrix ( new Matrix ( cp ) . InnerSimplified ) ;
221
- }
220
+ } , this ) ;
221
+ private FieldCache < Matrix ? > inverse ;
222
222
223
- /// <summary>Inverts the matrix</summary>
224
- [ Obsolete ( "Use ComputeInverse() instead" ) ]
225
- public Matrix Inverse ( ) => ComputeInverse ( ) ;
226
223
227
224
/// <summary>
228
225
/// The Add operator. Performs an active operation
@@ -270,22 +267,6 @@ public Matrix ComputeInverse()
270
267
}
271
268
}
272
269
273
- /// <summary>
274
- /// The Multiply operator. Performs an active operation.
275
- /// 1. Finds the inverse of the divisor
276
- /// 2. Multiplies the dividend by the inverse of the divisor
277
- /// and then applies inner simplification.
278
- /// The operator only works with square matrices of the same size
279
- /// </summary>
280
- /// <exception cref="InvalidMatrixOperationException">
281
- /// May be thrown if no inverse was found.
282
- /// </exception>
283
- public static Matrix operator / ( Matrix m1 , Matrix m2 )
284
- {
285
- var inv = m2 . ComputeInverse ( ) ;
286
- return m1 * inv ;
287
- }
288
-
289
270
/// <summary>
290
271
/// Performs a binary power of the matrix.
291
272
/// The matrix must be a square matrix.
@@ -369,11 +350,16 @@ public IEnumerator<Entity> GetEnumerator()
369
350
/// <summary>
370
351
/// Adjugate form of a matrix
371
352
/// </summary>
372
- public Matrix Adjugate =>
353
+ public Matrix ? Adjugate =>
373
354
adjugate . GetValue ( static @this =>
374
- ( Matrix ) new Matrix ( @this . InnerMatrix . Adjoint ( ) ) . InnerSimplified ,
355
+ {
356
+ if ( ! @this . IsSquare )
357
+ return null ;
358
+ var innerSimplified = new Matrix ( @this . InnerMatrix . Adjoint ( ) ) . InnerSimplified ;
359
+ return ToMatrix ( innerSimplified ) ;
360
+ } ,
375
361
this ) ;
376
- private FieldCache < Matrix > adjugate ;
362
+ private FieldCache < Matrix ? > adjugate ;
377
363
378
364
/// <summary>
379
365
/// Returns a vector, where the i-th element
0 commit comments