Robot Control Library
rc_test_matrix.c
/**
* @example rc_test_matrix.c
*
* @brief Tests the functions in rc_matrix.h
*
* @author James Strawson
* @date 1/29/2018
*/
#include <stdio.h>
#include <rc/math.h>
#define DIM 3 // dimension of matrix to test
int main()
{
double det;
printf("Let's test some matrix functions....\n\n");
// zeros matrix
printf("\nzeros Matrix:\n");
rc_matrix_zeros(&A,DIM,DIM);
// identity matrix
printf("\nIdentity Matrix:\n");
// random vector
printf("\nNew Random Vector b:\n");
// diagonal matrix
printf("\nDiagonal Matrix from b:\n");
// random matrix
printf("\nNew Random Matrix A:\n");
rc_matrix_random(&A,DIM,DIM);
// duplicate matrix
printf("\nDuplicate A into B:\n");
// print scientific notation
printf("\nMatrix B in sci notation:\n");
// times scalar
printf("\nMatrix B times 2.0:\n");
// get determinant of A & B
printf("\nDeterminant of A:");
printf("%8.6lf\n", det);
printf("\nDeterminant of B:");
printf("%8.6lf\n", det);
// multiply A*B=C
printf("\nThree ways to multiply:");
printf("\nA*B=C:\n");
// left multiply in place
printf("\nleft multiply inplace B=A*B\n");
// right multiply in place
printf("\nright multiply inplace A=A*B\n");
// add
printf("\ntwo ways to add:");
printf("\nA+B=C:\n");
rc_matrix_add(A,B,&C);
printf("\ninplace: A=A+B:\n");
// transpose
printf("\ntwo ways to transpose:");
printf("\nC=A':\n");
printf("\ninplace: A=A'\n");
// multiply b times A
printf("\nrow vector b times A:\n");
// multiply A times b
printf("\nA times column vector b\n");
// outer product
printf("\nouter product C=b*y\n");
printf("\nDONE\n");
return 0;
}
int rc_matrix_zeros(rc_matrix_t *A, int rows, int cols)
Resizes matrix A and allocates memory for a matrix with specified rows & columns. The new memory is p...
int rc_matrix_diagonal(rc_matrix_t *A, rc_vector_t v)
Generates a diagonal matrix with the elements of specified vector v.
#define RC_MATRIX_INITIALIZER
Definition: matrix.h:39
int rc_matrix_left_multiply_inplace(rc_matrix_t A, rc_matrix_t *B)
Multiplies A*B and puts the result back in the place of B.
int rc_matrix_identity(rc_matrix_t *A, int dim)
Resizes A to be a square identity matrix with dimensions dim-by-dim.
int rc_matrix_add(rc_matrix_t A, rc_matrix_t B, rc_matrix_t *C)
Adds matrices A+B and places the result in C.
int rc_matrix_times_col_vec(rc_matrix_t A, rc_vector_t v, rc_vector_t *c)
Multiplies matrix A times column vector v and places the result in column vector c.
int rc_matrix_multiply(rc_matrix_t A, rc_matrix_t B, rc_matrix_t *C)
Multiplies A*B=C.
int rc_matrix_print(rc_matrix_t A)
Prints the contents of matrix A to stdout in decimal notation with 4 decimal places.
int rc_matrix_add_inplace(rc_matrix_t *A, rc_matrix_t B)
Adds matrix A to B and places the result back in A.
int rc_matrix_times_scalar(rc_matrix_t *A, double s)
Multiplies every entry in A by scalar value s.
int rc_matrix_row_vec_times_matrix(rc_vector_t v, rc_matrix_t A, rc_vector_t *c)
Multiplies row vector v times matrix A and places the result in row vector c.
int rc_matrix_duplicate(rc_matrix_t A, rc_matrix_t *B)
Duplicates the contents of matrix A and into matrix B.
int rc_matrix_transpose(rc_matrix_t A, rc_matrix_t *T)
Transposes the contents of A and places the result in T.
int rc_matrix_transpose_inplace(rc_matrix_t *A)
Transposes matrix A in place.
int rc_matrix_outer_product(rc_vector_t v1, rc_vector_t v2, rc_matrix_t *A)
Computes v1 times v2 where v1 is a column vector and v2 is a row vector.
int rc_matrix_right_multiply_inplace(rc_matrix_t *A, rc_matrix_t B)
Multiplies A*B and puts the result back in the place of A.
int rc_matrix_random(rc_matrix_t *A, int rows, int cols)
Generates a matrix populated with random numbers between -1 and 1.
int rc_matrix_print_sci(rc_matrix_t A)
Prints the contents of matrix A to stdout in scientific notation.
double rc_matrix_determinant(rc_matrix_t A)
Calculates the determinant of square matrix A.
int rc_vector_random(rc_vector_t *v, int length)
Resizes vector v and fills with random numbers between -1.0 and 1.0.
#define RC_VECTOR_INITIALIZER
Definition: vector.h:48
int rc_vector_print(rc_vector_t v)
Prints to stdout the contents of vector v in one line.
Struct containing the state of a matrix and a pointer to dynamically allocated memory to hold its con...
Definition: matrix.h:32
Struct containing the state of a vector and a pointer to dynamically allocated memory to hold its con...
Definition: vector.h:41