My application would perform a large number of matrix operations (e.g., add/ multiply) on dense matrices. I would like to cache unique results in order to avoid duplicate computations.
Dense matrix:
typdef struct denseMatrix{
int m;
int n;
double **d; // actual matrix
multiplyTable **entry; // key & result
} dns;
Table-entry:
typedef struct multiplyTable{
dns *rightOperand; // key
dns *result; // value
} multiplyTable; // or something like that
dns *A, *B, *C, *D...; // allocated internally
C = mult(A,B); //may be called many many times.
In this case, mult would add an entry (operand, result) pair to the table
add(A->entry, B, C); //B is the right operand and C is the result
Later if D = mult(A, B) were to be called again, search(A->entry,B) would retrieve C. If on the other hand, the specific operand is not in the list, it would be added along with the pointer to the result matrix.
I have never done anything like this before and I am not even sure if this is the way to approach the problem. From my limited understanding, hash-tables may be used to implement something like this.
Among the practical questions I have are: (a) Is hash-table the appropriate solution to the problem in the first place? Do they allow pointer addresses as keys and values??
(b) Does it make sense to keep the "hash-table" as a "field" in the struct? That way, I already have the left operand, I just need to search for the rightOperand in the multiplication table. Or, should there be an independent table with both the left and right operands as keys?
(c) Do I create separate tables for addition/multiplication etc., or should there be a single table, with operands and operators?
(d) Wht would be the best way of keeping track of all the objects that are created so that these could be freed appropriately??
(e) What publicly available library (in c) would be suitable for implementing something like this?
I am seeking input/suggestions regarding (a) alternative ways in which the problem could be approached, and (b) advantages/disadvantages of such alternatives.
Lastly, I have found this forum to be immensely helpful and wanted to express my gratitude. ++Thanks.