- Home /

# Matrix Multiplication, Standard Deviant and More usefull methods

I'm trying to create a Artificial Neural Network to make a evolving AI for a simple game.

But I'm struggling with the math required by it.

I made a neural net in Python, in around 100 lines, but when I tried to port it to c# I'm already at 200 lines of code, and I haven't even begin the back propagation algorithm (the bulk of the code in python).

For instance a simple input normalization in python takes 1 line:

```
self.input_values = (inputs - np.mean(inputs , axis=0)) / np.std(inputs , axis=0)
```

While in C# I've to do it step by step:

```
float[] normalize_input(float[] inputs){
float sum = 0.0f;
for (int i = 0; i < inputs.Length; i++) {
sum += inputs[i] ;
}
float average = sum / inputs.Length;
float[] deviations = new float[inputs.Length];
for (int i = 0; i < inputs.Length; i++) {
deviations[i] = Mathf.Pow(inputs[i] - average,2);
}
float sum_deviation = 0;
for (int i = 0; i < deviations.Length; i++) {
sum_deviation += deviations[i];
}
float variance = sum_deviation / deviations.Length;
float std = Mathf.Sqrt (variance);
for (int i = 0; i < inputs.Length; i++) {
inputs[i] = (inputs[i] - average)/std;
}
return inputs;
}
```

**Is there any way to have access to this sort of math functions and matrix computation?**

I know I can do it step by step like I'm doing, but it will take a lot longer, and further more, it's very hard to debug once something is off.

**Answer** by Baste
·
May 11, 2015 at 08:20 AM

You have the Mathf library which have some formulas. There's also the default C# Math library.

Other than that, you should just define all of the helpers you need. It's not very hard to define arbitrary-length mean methods, or matrix multiplication methods.

For example, writing just `float arraySum(float[] A) { ... }`

would eli$$anonymous$$ate two of your loops (I think C# has some sort of extension that does that, but not worth learning just for this.)

**Answer** by tanoshimi
·
May 11, 2015 at 08:21 AM

I'm not that experienced with Python, but if you want to write C# code that handles vectors and matrices in a similar way to, say, MATLAB or Octave, you can try http://numerics.mathdotnet.com/ - I believe the Normalize() method will then provide the single-line method you're looking for.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.