Skip to content

Commit cc01817

Browse files
committed
Add public version of tm_cond()
1 parent 77f68f5 commit cc01817

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/lib/tmatrix.h

+7
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,12 @@ int tm_rank(tMat *m, int *err);
299299
* @return Square norm.
300300
*/
301301
tmVal tm_norm2(tMat* m, int* err);
302+
/**
303+
* @brief Find condition number of the matrix.
304+
* @param m matrix object.
305+
* @param err error code.
306+
* @return condition number.
307+
*/
308+
tmVal tm_cond(tMat* m, int* err);
302309

303310
#endif /* T_MATRIX_H */

src/lib/tmatrix_calc.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#define PINV_TOL 1E-9
1515
#define MEM_TMP_VEC 8
1616

17-
/* Condition number */
18-
tmVal tm_cond(tMat *m, tMat *minv, int *err)
17+
/* Condition number (internal) */
18+
tmVal tm_cond_(tMat *m, tMat *minv, int *err)
1919
{
2020
int e = 0, rA;
2121
tmVal cond = 0.0;
@@ -319,7 +319,7 @@ tmVal tm_inv(tMat *dst, tMat *m, int *err)
319319
lubksb(&tmp,idx,&col);
320320
}
321321
// check condition number
322-
cond = tm_cond(m, dst, &e);
322+
cond = tm_cond_(m, dst, &e);
323323
if(!e && !(cond > 0.0))
324324
e = TM_ERR_NO_SOLUTN;
325325
}
@@ -338,6 +338,25 @@ tmVal tm_inv(tMat *dst, tMat *m, int *err)
338338
return cond;
339339
}
340340

341+
/* Find condition number */
342+
tmVal tm_cond(tMat* m, int* err)
343+
{
344+
int e = 0;
345+
tmVal cond = 0.0;
346+
/* allocate memory */
347+
tMat minv = tm_copy(m, &e);
348+
349+
if(!e) {
350+
/* condition number is calculated inside the tm_inv function */
351+
cond = tm_inv(&minv, m, &e);
352+
}
353+
354+
if(err) *err = e;
355+
tm_clear(&minv);
356+
357+
return cond;
358+
}
359+
341360
/* Pseudoinverse aux 1 */
342361
tMat pinva(tMat *src, int* transp, tmVal* tolerance, int* err)
343362
{

src/tests/tests.c

+3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ static char* test_inv()
311311
cond = tm_inv(&im, &m, &err);
312312
mu_check("Inv (det):", err);
313313
mu_assert("Inv (inv): bad condition number", cond > 0 && cond < 1E14);
314+
315+
mu_assert("Inv (cond): wrong condition number", EQL(cond, tm_cond(&m, &err)));
316+
mu_check("Inv (cond):", err);
314317
pr = tm_simp();
315318

316319
tm_mul(&pr,&m,&im,&err);

0 commit comments

Comments
 (0)