# Math Help - A Code Optimization Problem

1. ## A Code Optimization Problem

Let's say I have a matrix of cases. A row of the matrix corresponds to a sensor in a physical system, and a column of the matrix corresponds to a state that the system can be in. The entries in the matrix itself correspond to, say, a safe high limit for the sensor in that row. Here's an example:

http://quicklatex.com/cache3/ql_9c51...d65d09a_l3.png

The code used to produce this table is as follows:

\begin{tabular}{c|ccc}&\text{Off} &\text{Start} &\text{Run}\\ \hline \text{Temp} &90^{\circ}\text{C} &100^{\circ}\text{C} &150^{\circ}\text{C}\\ \text{Pressure} &10 \text{psig} &20 \text{psig} &30 \text{psig}\end{tabular}

Now, then. I need to create nested case structures to handle outputting these limits to the checking routine. I want to minimize the total number of cases I have to code by altering the nesting of the cases. I can either check by state first, or by sensor first. For example, if the rows were "more uniform", then it would be better to have the state checking nested inside the sensor checking thus:

Code:
case sensor
Temp
case state
Off
Start
Run
end case
Pressure
case state
Off
Start
Run
end case
end case
That way, I could have fewer outer entries (I can combine cases if they're the same). On the other hand, if the columns were "more uniform", then it would be better to reverse the nesting order thus:

Code:
case state
Off
case sensor
Temp
Pressure
end case
Start
case sensor
Temp
Pressure
end
Run
case sensor
Temp
Pressure
end case
end case
So I was thinking about a way to quantify whether the rows or columns are "more uniform". Here's my thought: to measure the uniformity of the columns, take each column, find its cardinality (the number of different items in that column), and sum over all the column cardinalities. To measure the uniformity of the rows, take each row, find its cardinality, and sum over all the row cardinalities.

If the sum of the row cardinalities is smaller than the sum of the column cardinalities, then have the row cases on the outside. If the sum of the row cardinalities is greater than the sum of the column cardinalities, then have the row cases on the inside.

Here's my question: will this procedure minimize the number of cases I have to code?

Thank you!

2. First, if you change "tabular" into "array" in the LaTeX code, then it works in MHF.

If the sum of the row cardinalities is smaller than the sum of the column cardinalities, then have the row cases on the outside. If the sum of the row cardinalities is greater than the sum of the column cardinalities, then have the row cases on the inside.

Here's my question: will this procedure minimize the number of cases I have to code?
I think your measure correctly determines the total number of cases if there is an outer case for each column or for each row (and the inner case may combine rows or columns when cells have the same values). This measure would not necessarily return the absolute minimum of the number of cases. For example, if the n x n matrix has the form

$\begin{array}{cc}A & B\\B & A\end{array}$

where A and B are n/2 x n/2 submatrices where all elements are equal, then your measure will return 2n both for columns and for rows; however, it is enough to have 4 distinct cases.

I think that your measure is good unless you have a lot of special matrices and the need for optimal solution is high.