Converting my C# code into a mathematical formula

644 Views Asked by At

How can I convert my C# code into a mathematical formula? I would like to use discrete mathematics and convert my code into a mathematical formula.

Here is the example C# code that I have written. The aim is to calculate a rank based on the positive and negative weight that is assigned to each variable. The positive weight is added to the rank if the variable is higher than 0. The negative weight is taken away from the rank if the variable is a negative number. Please keep in mind that each variable can have any negative or positive weight that can be configured differently.

var aPosWeight = 3;
var aNegativeWeight = -3;

var bPosWeight = 2;
var bNegativeWeight = -2;

var cPosWeight = 1;
var cNegativeWeight = -1;

var a = 1;
var b = 0;
var c = -1;

var rank = 0;


if (a > 0)
{
    rank += aPosWeight;
}
else if (a < 0)
{
    rank += aNegativeWeight;
}

if (b > 0)
{
    rank += bPosWeight;
}
else if (b < 0)
{
    rank += bNegativeWeight;
}

if (c > 0)
{
    rank += cPosWeight;
}
else if (c < 0)
{
    rank += cNegativeWeight;
}

Console.WriteLine(string.Format("Your final rank is {0}", rank));

Here is the mathematical formula that I have come up with that I think represents the C# code written above. My main concern is that the mathematical formula is completely wrong and can't be used to interpret the C# code that I've written above.

$$ f(x)=\begin{cases}\text{PosWeight},\quad\text{if }a>0\\ \text{NegWeight},\quad\text{if }a<0\\ 0\hphantom{egw.eight},\quad\text{if }a=0\end{cases} $$

$$ \text{sum}=\sum_{i=1}^3f(x_i) $$

4

There are 4 best solutions below

0
On

Let $p,\,n$ denote the positive and negative weights, so $f(x)$ is $p[x>0]+n[x<0]$ in terms of Iverson brackets.

0
On

Let $$\text{weight}(x) = \begin{cases} \text{xPosWeight} & x > 0\\ 0 & x = 0\\ \text{xNegWeight} & x < 0 \end{cases}$$

As J.G. mentions in their answer, you can write this same function somewhat succinctly as

$$\text{weight}(x) = \text{xPosWeight}[x > 0] + \text{xNegWeight}[x < 0]$$

if you're familiar with the Iverson Bracket.

Then your code outputs $$\text{rank} = \sum_{x} \text{weight}(x)$$

As in code, you would need to define $\text{xPosWeight}$ and $\text{xNegWeight}$ for each $x$ you're interested in.


I hope this helps ^_^

0
On

First of all, a C# function is a mathematical function. It just is somewhat verbose. For writing your function more succinctly, you are on the write track. You can write:

$$f(x) = \begin{cases} x > a & a^+ \\ x < a & a^- \\ x = a & 0 \end{cases} + \begin{cases} x > b & b^+ \\ x < b & b^- \\ x = b & 0 \end{cases} + \begin{cases} x > c & c^+ \\ x < c & c^- \\ x = c & 0 \end{cases} $$

or more succinctly as $$ f(x) = \sum_{k \in \{a, b, c\}} \begin{cases} x > k & k^+ \\ x < k & k^- \\ x = k & 0 \end{cases} $$

You could also use the Heavyside step function, $$H[n] = \begin{cases} n \ge 0 & 1 \\ n < 0 & 0 \end{cases}$$

if you don't care about readability and only want show that it could be written this way to write it like $$f(x) = \sum_{k \in \{a, b, c\}} (1 - H[k - x])k^+ + (1 - H[x - k])k^-$$

0
On

If you're trying to do this automatically, you could write a parser and input the specific 'final value' and then determine how it was composed by building a tree of derivation steps and then look at how it was used, the existence of datasets etc and tie these activities to mathematical symbols.

eg.

float inputa, inputb, finalvalue; .. ..

for (int x=0; x < 5; x++) { inputa = inputb *x; finalvalue+=inputb; }

where finalvalue is the derived value.

in this case the for loop would look like a sum (sigma) from 0 to 5 with bx as its term.

anyway, after doing this you could use a latex renderer to create a graphic of your equation, like they're doing above with their answers.