# Plot a gaussian distribution in Solidity

are there any libraries to plot a gaussian distribution, and measure position along that curve? like https://github.com/errcw/gaussian in javascript

• Certainly you can make heavy calculations in solidity, but it will be expensive and consume lots of resources. Solidity and blockchain are not designed with heavy computation in mind. Also you are limited by the block gas limit that put a cap on how much calculation can be done in a single transaction. Usually heavy computation is done off-blockchain and only minimal is done with solidity, perhaps to store proof of the work done. – Ismael May 25 '18 at 16:44
• gaussian distributions are not that much calculations, just plot-points, total, mean, difference, variance, more or less. if there was a library I would use that, but I can write it myself also probably – graphoRa May 25 '18 at 17:24
• Gaussian distributions are not plot points, the bell shape in a Gaussian distribution comes for the calculation of an exponential function, the calculation over the domain of the independent variable is easily performed in a pc, but in Etehreum every operation cost ether! – Jaime May 25 '18 at 18:41
• wrote an answer a few minutes prior to your comment, feel free to see if it does not work ethereum.stackexchange.com/questions/49516/… – graphoRa May 25 '18 at 18:44
• So you solved your problem, good!. If you did so, accept your own answer. Good luck. – Jaime May 25 '18 at 19:31

Solidity is used to write code for the Ethereum Virtual Machine, is not used to write pc programs or control GUI. You can find an introduction to Block-chain, Etehreum and the EVM here

Hope this help

• yes I know. are there libraries for normal distribution graphs? – graphoRa May 25 '18 at 15:27
• No, there isn't. – Jaime May 25 '18 at 15:29
• You can't plot anything using solidity. As it was said, it just execute basic code with (relatively) basic logic. – Florian Castelain May 25 '18 at 15:29
• assembly, turing complete, etc – graphoRa May 25 '18 at 15:35
• I guess you could do it. But it would be like writing a device driver in typescript, or 3D graphics code in PL/SQL. It's the wrong use for the tool. No one is working on libraries to misuse Solidity. – Ether Dude May 25 '18 at 19:33

I think something like this here is enough for a gaussian distribution,

``````contract gaussianDistribution {

struct BellCurve {
uint population;
uint total;
uint mean;
uint standardDeviation;
uint differences_squared_sum;
uint variance;
}

BellCurve bellcurve;

function calculateMean() {
require(bellcurve.mean == 0);
bellcurve.mean = bellcurve.total / bellcurve.population;
}

require(bellcurve.variance == 0);
uint mean = bellcurve.mean;
require(mean != 0); // Has the mean been calculated first?
uint plot = bellcurve.plot[_node];
require(plot != 0); // Is the person in the record?
require(bellcurve.differences[_node] == 0); // Has this person been processed already?
uint difference = mean - plot;
bellcurve.differences[_node] = difference;
bellcurve.differences_squared_sum += difference^2;
}

function calculateVariance() {
uint variance = bellcurve.differences_squared_sum / bellcurve.population;
bellcurve.variance = variance;
}

function calculateStandardDeviation() {
uint variance = bellcurve.variance;
require(variance != 0);
bellcurve.standardDeviation = sqrt(bellcurve.variance);
}

function sqrt(uint x) returns (uint y) {
uint z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}

}
``````
• An issue you might have is that since you are using `uint` for the difference it will overflow if the result is negative. – Ismael May 28 '18 at 3:32