public class neural_network
{
	
public int MPNeuron(double x, double y, double wn[])
{
// 1. The function MPneuron() models the McCulloch-Pitts neuron.
// The function receives the following parameters:
// x is the value of the independent variable
// y is the value of the dependent variable
// wn[] is an array of three weights
// mpNeuron() computes:
// f(x) = f((w[0] * x1) + (w[1] * x2) + (w[2] x bias))
// where bias is a constant with the value 1.0
// Returns:
// if f(x) is positive, lpNeuron() returns 1
// if f(x) is negative, lpNeuron() returns -1
//
double bias=1.0;
double result = 0;
// Routine logic, per McCulloch-Pitts model
result = (wn[0] * x) + (wn[1] * y) + (wn[2] * bias);
if(result < 0)
return -1;
else
return 1;
}
	
public int NeuronB(int numValues, // number of data elements
int weights[], // array of weights
int values[]) // array of inputs
{
// Computes sum-of-products of = values[x] * weight[x]
// for numValues number of array elements
// then applies the threshold function:
// if(sum < 0)
// output = 0
// else
// output = 1
int result = 0;
for(int j = 0; j < numValues; j++)
result += weights[j] * values[j];
if(result < 0)
return 0;
else
return 1;
}

public int LogicAnd(int x, int y)
{
// Pre conditions:
// 1. Receives values of x and y in integer variables. Caller
// is responsible for ensuring binary values only (0 or 1)
// 2. Function sets weights and bias line for performing logical
// AND operation, using a neuron that follows the
// McCulloch-Pitts model
// Post conditions:
// Returns logical 0 or 1
int weights[]=new int[3]; // Array for weights
int inputs[]=new int[3];  // Array for inputs
// Set weights
weights[0] = -2;          // Bias line weight
weights[1] = 1;
weights[2] = 1;
inputs[0] = 1;            // Bias line input
inputs[1] = x;
inputs[2] = y;
// Call Neuron()
return (NeuronB(3, weights, inputs));
}

public int LogicOr(int x, int y)
{
// Pre conditions:
// 1. Receives values of x and y in integer variables. Caller
// is responsible for ensuring binary values only (0 or 1)
// 2. Function sets weights and bias line for performing logical
// AND operation, using a neuron that follows the
// McCulloch-Pitts model
// Post conditions:
// Returns logical 0 or 1
int weights[]=new int[3]; // Array for weights
int inputs[]=new int[3];  // Array for inputs
// Set weights
weights[0] = -1;          // Bias line weight
weights[1] = 1;
weights[2] = 1;
inputs[0] = 1;            // Bias line input
inputs[1] = x;
inputs[2] = y;
// Call Neuron()
return (NeuronB(3, weights, inputs));
}
public int LogicNot(int x)
{
// Pre conditions:
// 1. Receives values of x and y in integer variables. Caller
// is responsible for ensuring binary values only (0 or 1)
// 2. Function sets weights and bias line for performing logical
// AND operation, using a neuron that follows the
// McCulloch-Pitts model
// Post conditions:
// Returns logical 0 or 1
int weights[]=new int[3]; // Array for weights
int inputs[]=new int[3];  // Array for inputs
// Set weights
weights[0] = 0;          // Bias line weight
weights[1] = -2;
weights[2] = 1;
inputs[0] = 1;            // Bias line input
inputs[1] = x;
inputs[2] = 0;
// Call Neuron()
return (NeuronB(3, weights, inputs));
}


public int LogicXor(int weights[], int values[])
{
// This function performs a logic XOR using a network based on the
// following relationship:
//
// x XOR y = (NOT(x) AND y) OR (NOT(y) AND x)
//
// Stage 1:
// calculate outputA = NOT(x) AND y
// outputB = NOT(y) AND x
// Stage 2:
// calculate result = outputA OR outputB
int temp[] = {0, 0};
int result;
int outputA, outputB;
// Save data in temp[] array
temp[0] = values[0];
temp[1] = values[1];
// Call LogicNot() and LogicAnd()operations, as follow:
// outputA = NOT(x) AND y
// outputB = NOT(y) AND x
values[0] = LogicNot(weights, values); // NOT x
outputA = LogicAnd(weights, values); // x AND y
// Invert x and y
values[0] = temp[1]; // Variable y
values[1] = temp[0];
// ASSERT:
// values[0] = y
// values[1] = x
values[0] = LogicNot(weights, values); // NOT y
outputB = LogicAnd(weights, values); // x AND y
// ASSERT:
// Stage 1 concluded
// State 2:
// perform outputA OR output B
values[0] = outputA; // Update values[] array
values[1] = outputB;
result = LogicOr(weights, values); // Perform OR
return result;
}

}