I am adapting a program which compiles OK; but when I try to run it I get an error window that says "no main classes found". I searched on your site for that type of problem (for NetBeans)and then tried a R click on my project in the Project window. Lagrange/properties/run and the class shown was the one in my program. I clicked run in that window and got the same error message.
The program is pasted below:
package lagrange;
class MyMath {
double xi[] = { 0, 0.5, 1, 1.5, 2 };
double fi[] = { 1, 0.938470, 0.765198, 0.511828, 0.223891 };
double x = 0.9;
double f = aitken(x, xi, fi);
// Method to carry out the Aitken recursions.
public double aitken(double x, double xi[], double fi[]) {
int n = xi.length - 1;
double ft[] = (double[]) fi.clone();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - i; ++j) {
ft[j] = (x - xi[j]) / (xi[i + j + 1] - xi[j]) * ft[j + 1]
+ (x - xi[i + j + 1]) / (xi[j] - xi[i + j + 1]) * ft[j];
}
}
return ft[0];
}
}
public class Lagrange {
public void main(String[] args) {
// TODO code application logic here
System.out.println("Interpolated value: " + f);
}
}
It is public static void main - main method has to be static. And in your current code, you are declaring the main method in an inner class which is not static. This is not allowed and it will fail as
static methods can only be declared in a static or top level type.
One solution that can work for you is provided below with these changes -
main method shifted to MyMath class.
main method being static does not have access to non-static members and hence an instance of MyMath is created and used to print the result.
package lagrange;
class MyMath {
double xi[] = { 0, 0.5, 1, 1.5, 2 };
double fi[] = { 1, 0.938470, 0.765198, 0.511828, 0.223891 };
double x = 0.9;
double f = aitken(x, xi, fi);
// Method to carry out the Aitken recursions.
public double aitken(double x, double xi[], double fi[]) {
int n = xi.length - 1;
double ft[] = (double[]) fi.clone();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - i; ++j) {
ft[j] = (x - xi[j]) / (xi[i + j + 1] - xi[j]) * ft[j + 1]
+ (x - xi[i + j + 1]) / (xi[j] - xi[i + j + 1]) * ft[j];
}
}
return ft[0];
}
public static void main(String[] args) {
System.out.println("Interpolated value: " + new MyMath().f);
}
}
Related
What's wrong with my progam?
public class Square{
public int x;
public Square() {
int x[] = new int[10];
int y;
x[0] = 7;
}
public void root() {
for (int i = 0; i < 10; i++) {
x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
System.out.println(x[i + 1]);
}
}
}
I don't get what's wrong, my for loop does not seem to be working and it keeps on displaying the error for some reason. Could someone help me figure this out?
Okay, I wrote this program now:
public class Square
{
public double x[];
public void root()
{
double x[] = new double[10];
x[0]=7;
for(int i=0; i<8; i++)
{
x[i+1]=x[i]-(Math.pow(x[i]-2.5,2))/(2*(x[i]-2.5));
System.out.println(x[i+1]);
}
}
}
And it is showing this output:
3.625
3.0625
2.78125
2.640625
2.5703125
2.53515625
2.517578125
java.lang.NullPointerException
at Square.root(Square.java:14)
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at Square.root(Square.java:11)
I don't know why I'm getting these errors. Also, the answer should be 6.25 at some point. But, it doesn't show that output.
Your constructor has a local variable int[] x which is disarded at the end of the constructor.
Try this:
public class Square{
// initialize to array of ten ints
public int x[] = new int[10];
public Square() {
x[0] = 7;
}
public void root() {
for (int i = 0; i < 10; i++) {
x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
System.out.println(x[i + 1]);
}
}
}
Edit: The int y is local to the constructor, it is discarded at the end of the constructor scope, too.
This is because you have defined x previously as just an int, instead of an array of ints.
Try this:
public class Square {
public int x[];
public Square() {
this.x = new int[10];
int y;
x[0] = 7;
}
public void root() {
for(int i = 0; i < 10; i++) {
x[i + 1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] -2.5));
System.out.println(x[i + 1]);
}
}
}
Here is the commented code explaining the problem :
public class Square
{
// Here, x is defined as an attribute of class Square, of type int
public int x;
public Square()
{
// Here, x is locally defined as a local variable, of type int[]
// It shadows the attribute x. This is considered as a bad practice.
int x[] = new int[10];
int y;
// Here, x is the local variable, of type int[]. It IS an array so
// this line is valid.
x[0]=7;
}// From this point, the local variable x is not defined anymore
// (that is the point of a local variable)
Now here :
public void root()
{
for(int i=0; i<10; i++)
{
// Here, x is referencing the attribute of class Square, which is an int
// But you try to access it as if it was and int[]
x[i+1]
First you must declare x as an array:
public int[] x;
notice that the java style is not int x[];
Then inside Square() you have to initialize x like this:
x = new int[10];
Finally, this:
(Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5))
returns a double so you have to cast it to int:
x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));
So your code should be:
public int[] x;
public void Square() {
x = new int[10];
x[0] = 7;
}
public void root() {
if (x == null)
Square();
for(int i = 0; i < x.length - 1; i++) {
x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));
System.out.println(x[i+1]);
}
}
Inside the loop you are accessing the i + 1 item so the counter of the loop must take values up to x.length - 2, this is why I have in the code: i < x.length - 1.
I have removed the declaration of y from Square() as it is not used.
public class Square
{
public double x[];
public Square()
{
this.x = new double[10];
x[0]=7;
}
public void root()
{
System.out.println(x[0]);
for(int i=1; i<10; i++)
{
x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
System.out.println(x[i]);
}
}
}
Try use this code
public class Square
{
public double x[];
public void root()
{
x = new double[10];
x[0]=7;
for(int i=1; i<10; i++)
{
x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
System.out.println(x[i]);
}
}
}
So I've got 2 Mains and I'm trying to combine them to run together and struggling. The 1st rolls a single die 6000 times and shows the results and the second rolls 2 dice 11000 times and shows the results. What is the easiest way to combine these so they run 1 and than the other. Programs as follows:
public class DieTest
{
public static final int N = 6000;
public static void main(String[] args)
{
int[] d = new int[7];
for (int i = 1; i < 7; i++) d[i] = 0;
for (int k = 0; k < N; k++)
{
int roll = (int)(6.0*Math.random() + 1.0);
d[roll]++;
}
System.out.print("Rolls: " + N);
for (int i = 1; i < 7; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
and the second
public class Dice3
{
public static final int N = 11000;
public static int roll()
{
return (int)(6.0*Math.random() + 1.0);
}
public static void main(String[] args)
{
int[] d = new int[13];
for (int i = 1; i < 13; i++) d[i] = 0;
for (int k = 0; k < N; k++)
{
d[roll() + roll()]++;
}
System.out.print("Rolls: " + N);
for (int i = 2; i < 13; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
1.Create another class to combine the code.
2.Create 1 method for each of the 2 main() methods, of course with new names like roll1() and roll2() without parameters and paste inside them the code you already have.
3.Paste also the declarations of N from DieTest and Dice3 but rename the 2nd to M and change every occurrence of N to M in the 2nd method you created.
4.You need also to paste the method roll().
5.Create a new main() method like this:
public static void main(String[] args) {
roll1();
roll2();
}
public class TwoDieFor
{
public static void main(String ... arguments)
{
DieTest.main(arguments);
Dice3.main(arguments);
}
}
I can't seem to find what's wrong with my neural net, despite verifying my net based on this example, which suggests my backprop and forward prop is working fine. However, after training on XOR my net returns around 0.5 for the output regardless of the input. In other words, the net seems to be minimizing the error as best it can without seeing any correlation between the input and the output. Since a single iteration of back propagation seems to be working fine, my instinct would suggest the problem lies somehow in the iterations that follow. However, there isn't any obvious problem that would cause this, leaving me quite stumped.
I've looked at other threads where similar problems have arisen, but it seems most of the time their error is either extremely niche to the way they set up their net, or their parameters such as learning rate or epochs is really off. Is anyone familiar with a case like this?
public class Net
{
int[] sizes;
double LEARNING_RATE;
double[][][] weights;
double[][] bias;
Random rand = new Random(); //53489085
public Net(int[] sizes_, double LEARNING_RATE_)
{
LEARNING_RATE = LEARNING_RATE_;
sizes = sizes_;
int numInputs = sizes[0];
double range = 1.0 / Math.sqrt(numInputs);
bias = new double[sizes.length - 1][];
weights = new double[sizes.length - 1][][];
for(int w_layer = 0; w_layer < weights.length; w_layer++)
{
bias[w_layer] = new double[sizes[w_layer+1]];
weights[w_layer] = new double[sizes[w_layer+1]][sizes[w_layer]];
for(int j = 0; j < weights[w_layer].length; j++)
{
bias[w_layer][j] = 2*range*rand.nextDouble() - range;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
weights[w_layer][j][i] = 2*range*rand.nextDouble() - range;
}
}
}
}
public double[] evaluate(double[] image_vector)
{
return forwardPass(image_vector)[sizes.length-1];
}
public double totalError(double[][] expec, double[][] actual)
{
double sum = 0;
for(int i = 0; i < expec.length; i++)
{
sum += error(expec[i], evaluate(actual[i]));
}
return sum / expec.length;
}
private double error(double[] expec, double[] actual)
{
double sum = 0;
for(int i = 0; i < expec.length; i++)
{
double del = expec[i] - actual[i];
sum += 0.5 * del * del;
}
return sum;
}
public void backpropagate(double[][] image_vector, double[][] outputs)
{
double[][][] deltaWeights = new double[weights.length][][];
double[][] deltaBias = new double[weights.length][];
for(int w = 0; w < weights.length; w++)
{
deltaBias[w] = new double[bias[w].length];
deltaWeights[w] = new double[weights[w].length][];
for(int j = 0; j < weights[w].length; j++)
{
deltaWeights[w][j] = new double[weights[w][j].length];
}
}
for(int batch = 0; batch < image_vector.length; batch++)
{
double[][] neuronVals = forwardPass(image_vector[batch]);
/* OUTPUT DELTAS */
int w_layer = weights.length-1;
double[] deltas = new double[weights[w_layer].length];
for(int j = 0; j < weights[w_layer].length; j++)
{
double actual = neuronVals[w_layer + 1][j];
double expec = outputs[batch][j];
double deltaErr = actual - expec;
double deltaSig = actual * (1 - actual);
double delta = deltaErr * deltaSig;
deltas[j] = delta;
deltaBias[w_layer][j] += delta;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
deltaWeights[w_layer][j][i] += delta * neuronVals[w_layer][i];
}
}
w_layer--;
/* REST OF THE DELTAS */
while(w_layer >= 0)
{
double[] nextDeltas = new double[weights[w_layer].length];
for(int j = 0; j < weights[w_layer].length; j++)
{
double outNeur = neuronVals[w_layer+1][j];
double deltaSig = outNeur * (1 - outNeur);
double sum = 0;
for(int i = 0; i < weights[w_layer+1].length; i++)
{
sum += weights[w_layer+1][i][j] * deltas[i];
}
double delta = sum * deltaSig;
nextDeltas[j] = delta;
deltaBias[w_layer][j] += delta;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
deltaWeights[w_layer][j][i] += delta * neuronVals[w_layer][i];
}
}
deltas = nextDeltas;
w_layer--;
}
}
for(int w_layer = 0; w_layer < weights.length; w_layer++)
{
for(int j = 0; j < weights[w_layer].length; j++)
{
deltaBias[w_layer][j] /= (double) image_vector.length;
bias[w_layer][j] -= LEARNING_RATE * deltaBias[w_layer][j];
for(int i = 0; i < weights[w_layer][j].length; i++)
{
deltaWeights[w_layer][j][i] /= (double) image_vector.length; // average of batches
weights[w_layer][j][i] -= LEARNING_RATE * deltaWeights[w_layer][j][i];
}
}
}
}
public double[][] forwardPass(double[] image_vector)
{
double[][] outputs = new double[sizes.length][];
double[] inputs = image_vector;
for(int w = 0; w < weights.length; w++)
{
outputs[w] = inputs;
double[] output = new double[weights[w].length];
for(int j = 0; j < weights[w].length; j++)
{
output[j] = bias[w][j];
for(int i = 0; i < weights[w][j].length; i++)
{
output[j] += weights[w][j][i] * inputs[i];
}
output[j] = sigmoid(output[j]);
}
inputs = output;
}
outputs[outputs.length-1] = inputs.clone();
return outputs;
}
static public double sigmoid(double val)
{
return 1.0 / (1.0 + Math.exp(-val));
}
}
And my XOR class looks like this. It's very unlikely that the error lies in this part given it's simplicity, but I figured it couldn't hurt to post just in case I have some fundamental misunderstanding to how XOR works. My net is set up to take examples in batches, but as you can see below for this particular example I send it batches of one, or effectively not using batches.
public class SingleLayer {
static int numEpochs = 10000;
static double LEARNING_RATE = 0.001;
static int[] sizes = new int[] {2, 2, 1};
public static void main(String[] args)
{
System.out.println("Initializing randomly generate neural net...");
Net n = new Net(sizes, LEARNING_RATE);
System.out.println("Complete!");
System.out.println("Loading dataset...");
double[][] inputs = new double[4][2];
double[][] outputs = new double[4][1];
inputs[0] = new double[] {1, 1};
outputs[0] = new double[] {0};
inputs[1] = new double[] {1, 0};
outputs[1] = new double[] {1};
inputs[2] = new double[] {0, 1};
outputs[2] = new double[] {1};
inputs[3] = new double[] {0, 0};
outputs[3] = new double[] {0};
System.out.println("Complete!");
System.out.println("STARTING ERROR: " + n.totalError(outputs, inputs));
for(int epoch = 0; epoch < numEpochs; epoch++)
{
double[][] in = new double[1][2];
double[][] out = new double[1][1];
int num = (int)(Math.random()*inputs.length);
in[0] = inputs[num];
out[0] = outputs[num];
n.backpropagate(inputs, outputs);
System.out.println("ERROR: " + n.totalError(out, in));
}
System.out.println("Prediction After Training: " + n.evaluate(inputs[0])[0] + " Expected: " + outputs[0][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[1])[0] + " Expected: " + outputs[1][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[2])[0] + " Expected: " + outputs[2][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[3])[0] + " Expected: " + outputs[3][0]);
}
}
Can anyone provide some insight as to what may be wrong? My parameters are pretty well defined and I've followed all the suggestions for how the weights should be initialized and what the learning rate should be etc. Thanks!
You're only presenting the first 3 inputs to your neural network, because the following line is wrong:
int num = (int)(Math.random() * 3);
change that to
int num = (int)(Math.random() * inputs.length);
to use all 4 possible inputs.
I figured it out. I wasn't running enough epochs. That seems a little silly to me but this visualization revealed to me that the net lingers on answers ~0.5 for a long time before reducing the error to less than 0.00001.
#include <iostream>
using namespace std;
int fun(int *k);
int main() {
int i = 10, j = 10, sum1,sum2;
sum1 = (i / 2) + fun(&i);
sum2 = fun(&j) + (j / 2);
cout<< sum1<< " "<<sum2<<endl;
}
int fun(int *k) {
*k += 4;
return 3 * (*k) - 1;
}
I have to convert my code to Java Code I already converted but i couldn't find my mistake i cannot point variables to each other..
public class System{
public static void main(String[] args) {
int i = 10;
int j = 10;
int sum1 = (i / 2) + fun(k.value=i);
int sum2 = fun(k.value=j) + (j / 2);
System.out.println("%d%d",sum1,sum2 );
}
public static int fun(int k) {
intobj k;
int k= new k();
k.value += 4;
return 3 * (k.value) - 1;
}
}
This is my java code when i look at the int sum1 = (i / 2) + fun(k.value=i);
int sum2 = fun(k.value=j) + (j / 2); part isn't true about point to true values. How can i solve that pointers problem. Thank you.
The problem is that you're using ints instead of intobjs where you want to pass things around by reference (& in c++).
In your main function, you should try declaring i and j as intobjs and your parameter for fun, k should also be an intobj.
Code:
public class System{
public static void main(String[] args) {
intobj i = new intobj();
i.value = 10;
intobj j = new intobj();
j.value = 10;
int sum1 = (i.value / 2) + fun(i);
int sum2 = fun(j) + (j.value / 2);
System.out.println("%d%d",sum1,sum2 );
}
public static int fun(intobj k) {
k.value += 4;
return 3 * (k.value) - 1;
}
}
I've tried running this, but printing out circleCounter only prints 0. If I were to put the counter code under the tester function in the bottom part, then it would work. What am I doing wrong? Am I missing out on something?
public class Project1 {
public int circleCounter; // Number of non-singular circles in the file.
public int posFirstLast; // Indicates whether the first and last circles overlap or not.
public double maxArea; // Area of the largest circle (by area).
public double minArea; // Area of the smallest circle (by area).
public double averageArea; // Average area of the circles.
public double stdArea; // Standard deviation of area of the circles.
public double medArea; // Median of the area.
public int stamp = 189375;
public Project1() {
// This method is complete.
}
public void results(String fileName) {
MaInput F1 = new MaInput("DataFile.data");
double x, y, rad;
int circleCounter = 0;
double sumArea = 0;
Circle A = new Circle();
while (!F1.atEOF()) {
x = F1.readDouble();
y = F1.readDouble();
rad = F1.readDouble();
circleCounter++;
if (A.area() > maxArea) {
maxArea = A.area();
}
if (A.area() < minArea) {
minArea = A.area();
}
sumArea += A.area();
averageArea = sumArea / circleCounter;
stdArea = Math.sqrt((Math.pow(A.area() - averageArea, 2) / circleCounter));
//Array for points
Circle[] points = new Circle[circleCounter];
for (int j = 0; j < points.length; j++) {
if (rad > Point.GEOMTOL) {
points[j] = A;
}
}
posFirstLast = points[1].overlap(points[points.length]);
//Array of areas
double[] areas = new double[circleCounter];
for (int i = 0; i < areas.length; i++) {
if (rad > Point.GEOMTOL) {
areas[i] = A.area();
}
}
//Bubble Sort
for (int i = 0; i < areas.length; i++) {
if (areas[i + 1] < areas[i]) {
double temp = areas[i + 1];
areas[i + 1] = areas[i];
areas[i] = temp;
}
}
//Median
if (areas.length % 2 == 0) {
medArea = (0 / 5) * (areas[(areas.length / 2) - 1] + areas[areas.length / 2]);
} else {
medArea = (0.5) * (areas[((areas.length) - 1) / 2]);
}
}
}
public static void main(String args[]) {
Project1 pleasework = new Project1();
System.out.println("Number of (non-singular) circles: " + pleasework.circleCounter);
System.out.println("Whether the first and last circles overlap: " + pleasework.posFirstLast);
System.out.println("Maximum Area: " + pleasework.maxArea);
System.out.println("Minimum Area: " + pleasework.minArea);
System.out.println("Average Area: " + pleasework.averageArea);
System.out.println("Standard deviation of the areas: " + pleasework.stdArea);
System.out.println("Median of the areas: " + pleasework.medArea);
}
}
So, if it's only your circleCounter that's still giving you 0, then you should be aware of shadowing your variables.
private int circleCounter = 0; is applicable to the global scope.
int circleCounter = 0; is applicable to the scope local to your method results. The most local scope takes precedence with variables, so you've thus shadowed your global variable by redeclaring it here.
Simply take out that declaration and your variable won't be shadowed.
Edit: This also presumes that you actually call the method, too.
The main in your code does not invoke the results() method and hence all the default values of the fields are printed on your console i.e either 0 or 0.0(for double)
as main is the only entry point for java in your program.