- 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 eliminate 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How to do x^-1 1 Answer

Can't get Math Functions to Work in Compute Shader 0 Answers

How to get translation from matrix? 1 Answer

Events called every Nth day 1 Answer

RigidBody 2D x velocity function 1 Answer