How do I ensure that my fitness keep in range after mutation steps? - java

I use sphere benchmark function to test evolutionary strategy code with uncorrelate one step size mutation as follow:
public class cromosome {
private double[] variable = new double[2];
private double[] stepSize = new double[2];
private double fitness=0;
}
//=========================method set fitness=========================================
public void setFitness() {
for (int i=0; i<variable.length; i++) {
fitness += variable[i]*variable[i];
}
System.out.println("fitness= " + fitness);
}
my values are in range[-10;+10] after applying mutation steps its seems that my fitness is out of range.
this my mutation method
public static cromosome Mutation(cromosome cro) {
//Mutations with no correlation with one step size
Random r = new Random();
double a = r.nextGaussian();
double lr = 1 / (Math.sqrt(cro.getVariableLenght()));
double[] newMutationStep = new double[1];
newMutationStep[0] = cro.getMutationStep(0) * (Math.exp(lr * a));
double[] newVariable = new double[2];
for (int i = 0; i < cro.getVariableLenght(); i++) {
double b = r.nextGaussian();
newVariable[i] = cro.getVariable(i) + newMutationStep[0] * b;
}
cromosome newKromosom = new cromosome(newVariable[0], newVariable[1], newMutationStep[0]);
return newKromosom;
}
after many iteration the fitness values are out of range, how can I control my fitness ?

You are using r.nextGaussian(). The spread on this variable is larger than 1. Meaning you will sometimes get values below -1 or above 1. This is probably the source of the issue.
To fix it add a validation such as:
double a = r.nextGaussian();
if(a < -1){a = -1;}else if(a > 1){a = 1;}
After this handle it in the same way you are doing now.

Related

Java algorithm to split time in decreasing portions

I am trying to write a method in Java 7 to allocate times to a list.
For example:
if requiredConclusionTime - now is 1.75h and my list size is 3, then it should be allocated as follows:
item1 gets 1h
item2 gets 0.5h
item3 gets 0.25h
As you can see, the allocation is declining by a factor of half.
I have the following so far:
private void setRequiredConclusionTimes(ApprovalForm approvalForm, Date requiredConclusionTime) {
long currentTimeInMillies = new Date().getTime();
long requiredConclusionTimeInMillis = requiredConclusionTime.getTime();
long diff = requiredConclusionTimeInMillis - currentTimeInMillies;
List<List<Evaluator>> evaluatorsList = approvalForm.getEvaluatorsList();
for (Evaluator Evaluator : evaluatorsList.get(0)) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(fractionedTime); // this is incorrect and needs to be factored
approvalForm.addRequiredConclusionTimes(cal);
}
}
Each item would need a fraction of diff.
How do I calculate the correct fractionedTime for each item?
Thank you
Say n is the size of your list, then you can determine your chunking factor a as follows:
a = requiredConclusionTime/(2^n - 1).
Then list item i gets a * 2^i time.
See Geometric Progression at Wikipedia
What you essentiall want is to find x so that
is met, where t is the total time and n is the number of splits.
The sum can be reformulated as 2^(1-n)*(2^n - 1), so for x you would get
x then is the value for you first item and the second item would get x/2 and the third would get x/(2^2) and so on.
Knowing this you can write a method split as follows:
private static double[] split(double t, int n) {
double power2_1 = Math.pow(2.0, n - 1);
double power2 = Math.pow(2.0, n);
double factor = (t * power2_1) / (power2 - 1);
double[] res = new double[n];
for (int i = 0; i < n; i++) {
res[i] = factor;
factor /= 2;
}
return res;
}
Based on muued's answer, here is a simple GeometricProgression class which is just able to calculate your stuff, using BigDecimal:
public class GeometricProgression {
private final int sequenceSize;
private final BigDecimal chunkFactor;
public GeometricProgression(BigDecimal value, int sequenceSize) {
this.sequenceSize = sequenceSize;
BigDecimal divisor = BigDecimal.valueOf(2)
.pow(sequenceSize)
.subtract(BigDecimal.ONE);
this.chunkFactor = value
.divide(divisor, 8, RoundingMode.HALF_UP);
}
public BigDecimal get(int i) {
return this.chunkFactor.multiply(BigDecimal.valueOf(2).pow(i));
}
public void printSequence() {
for (int i = 0; i < this.sequenceSize; i++) {
System.out.print(get(this.sequenceSize - i - 1) + " ");
}
System.out.println();
}
public static void main(String[] args) {
new GeometricProgression(new BigDecimal("1.75"), 3).printSequence();
new GeometricProgression(BigDecimal.valueOf(6), 2).printSequence();
new GeometricProgression(BigDecimal.valueOf(6), 8).printSequence();
}
}

How to sort the array in ascending order?

I cannot figure out why the r array will not sort into ascending order. I have tried Array.sort and manually sorting the array.
import java.lang.*;
import java.lang.Object;
import java.lang.Integer;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Gaussian {
public static int seed;
public static final int n = 100;
public static void main(String argv[]) {
double r[] = new double[100];
// Initiate the seed from the current time
GregorianCalendar t = new GregorianCalendar();
int t1 = t.get(Calendar.SECOND);
int t2 = t.get(Calendar.MINUTE);
int t3 = t.get(Calendar.HOUR_OF_DAY);
int t4 = t.get(Calendar.DAY_OF_MONTH);
int t5 = t.get(Calendar.MONTH);
int t6 = t.get(Calendar.YEAR);
seed = t6 + 65*(t5+12*(t4+31*(t3+24*(t2+60*t1))));
if ((seed%2) == 0) seed = seed-1;
**************This is the section giving me trouble*****************
// Put the Gaussian random numbers in the array
for (int i=0; i<n-1; i+=1) {
r = rang();
for (int l=0; l<r.length-1; l++) {
if(r[l] < r[l+1]) {
double tempValue = r[l+1];
r[l+1] = r[l];
r[l] = tempValue;
}
}
System.out.println(r[0]);
******************Between these stars*******************************
}
}
// Method to create two Gaussian random numbers from two
// uniform random numbers in [0,1].
public static double[] rang() {
double x[] = new double[1];
double r1, r2;
r1 = - Math.log(1-ranf());
r2 = 2*Math.PI*ranf();
r1 = Math.sqrt(2*r1);
x[0] = r1*Math.cos(r2);
return x;
}
// Method to generate a uniform random number in [0,1]
// following x(i+1)=a*x(i) mod c with a=pow(7,5) and
// c=pow(2,31)-1. Here the seed is a global variable.
public static double ranf() {
final int a = 16807, c = 2147483647, q = 127773,
r = 2836;
final double cd = c;
int h = seed/q;
int l = seed%q;
int t = a*l-r*h;
if (t > 0) seed = t;
else seed = c+t;
return seed/cd;
}
}
For some reason it is giving me this output:
-0.7286443240313888
0.9205595151394262
-0.1201523471771766
-0.2955395834645375
0.5562293071303744
0.5947383124976592
-0.5190410499731951
1.1878905341959594
-0.6530738641804281
1.92941716216534
-1.55458771926982
1.011542837179014
-1.2973072313970084
-0.5115664645635027
-0.4537839981939878
-0.43386113937789456
2.1877083571592637
-0.1869725174568339
1.0427194985616417
0.7491392218512473
-0.2837863829399006
0.09204148771478798
0.08980225475596745
-1.0595943397788652
0.2493101533697332
-1.3926086961785766
0.9722238128294852
0.4490619874581054
1.4379635505387074
1.4550206564181973
-0.9754513444753741
-1.6454765651087158
0.1683214049373476
0.9981636099004854
-0.7289169766110786
1.6612385375332162
0.19025688479326378
0.0830947016802825
0.4674778575126086
-0.9077431792737849
-0.5638299547034225
0.13229202082089384
1.2429372493642745
-0.006685432080368285
2.336192098747748
-0.5450098522726261
-1.6420372037670146
0.3400579125911062
0.48689741262698993
-0.5075527810259783
1.9679760629290328
-1.9114919760463223
0.5633783650935041
0.12871665512520616
-1.8826404473732248
0.16725744941405607
1.049647212107755
0.767071049830706
0.3366261688045942
-1.726395330988362
-0.15241706234915703
-0.17645549457761323
1.098469368528642
-0.3173352964219553
-2.6584067823396675
0.4136264577634812
-1.2506808927401905
2.0462718170224186
-2.380899123430688
-1.0340941198026203
-3.223035072470854
-0.1423807157151033
-0.048464495873010084
1.4690537691472332
0.9110766995396362
-0.040683539673625924
-0.3895836309957472
-0.4793889976948361
0.007621022168540105
0.4151797552606307
1.2734508381903344
0.6398148976757589
-2.0458807284022114
0.23937728902415573
0.09380205942857836
1.331532378407905
-0.09813530948364875
0.9515533393393638
-1.5924626733327882
-1.2504131049626441
0.3674623983411812
0.8204457493547238
0.2214473639135442
0.5573901544532469
1.6349106235864332
-1.4373482822115147
0.38216369985059967
-0.6869980429363977
0.30632157455967757
Instead of sorting the numbers in ascending order.
The reason the the array is not sorting is because of r = rang();.
Initially, double r[] = new double[100]; sets r to an array of length 100. However, r is being reassigned to the result of rang(), which is an array the length of 1. This causes the inner for loop to never run.
I recommend the following changes:
// I'm assuming that you want 100 Gaussian random numbers, not 99
for (int i = 0; i < r.length; i += 1) {
// you could also change rang() to return a
// double instead of an array of double
// because the array only contains 1 double
double[] randomGaussian = rang();
r[i] = randomGaussian[0];
}
// sort the array
Arrays.sort(r);
// print out the array
for(int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
I figured it out guys!
// Put the Gaussian random numbers in the array
double copy[] = new double[n];
for (int i=0; i<n-1; i+=1) {
r = rang();
double temp = r[0];
copy[i] = temp;
}
// Sort the array
Arrays.sort(copy);
for (int i=0; i<n-1; i++) {
System.out.println(copy[i]);
}
Gives the right output!

Zipf's Law in Java for text generation - too slow

Hey there I'm working on a textgenerator, that should generate millions of different texts.
to make the content of each text realistic I used Zipf's Law
It is working well, the word distribution is correct.
But the following next() function is executing very slow and since I want to generate millions of articles it has to be changed. (The while loop is the slow part)
Can someone help me with this?
I implemented it like this:
public int next() {
int rank;
double frequency = 0;
double dice;
rank = rnd.nextInt(size);
frequency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
while (!(dice < frequency) || (rank == 0)) {
rank = rnd.nextInt(size);
frequency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
}
return rank;
}
EDIT: I obtainded the code from : http://diveintodata.org/2009/09/13/zipf-distribution-generator-in-java/
The implementation that you copied ... has some issues. One could probably say that it is plainly wrong, because it is using random values, and when in a computation like
rank = rnd.nextInt(size);
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
the rank value is 0, then the frequency is Infinity, and messes up some of the statistics.
I tried to correct these erros, but have not analyzed the implementation and have not compared it to the definition of the Zipf distribution function. So if somebody copies my code, he might find out that it still "...has some issues".
The implementation of the next function is, strictly speaking, not "total correct", in the sense that it does not necessarily terminate. There's nothing preventing the loop from running forever. Depending on the parameters, it may be more or less likely that it will take a while until it terminates. And I think that's also one of the main reasons for your "performance" issue: For some values, the condition (dice < frequency) is just very unlikely to happen....
Regardless of that, the goal that you want to achieve can be formulated more generically: You have a certain distribution of probabilities. And you want a "random" function that returns random values based on this distribution.
One simple and generic way to achieve this is to map the (cumulated) probability distribution to the target values with a NavigableMap. This map can then be used to quickly look up the target value, given a random value between 0.0 and 1.0 that is supplied by a java.util.Random instance.
There may be more efficient solutions for particular cases, but again: This is very generic and simple (and still, reasonably efficient).
I implemented this here for the Zipf distribution. Again, I did not verify everything in detail, and there are some +1/-1 oddities (mentioned in the first paragraph), but it should show the idea: The FastZipfGenerator fills the map containing the probability distribution, and in the next() function, just performs a lookup:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Random;
import java.util.TreeMap;
public class ZipfGeneratorTest
{
public static void main(String[] args) {
int size = 10;
double skew = 2.0;
ZipfGenerator z0 = new ZipfGenerator(size, skew);
FastZipfGenerator z1 = new FastZipfGenerator(size, skew);
long before = 0;
long after = 0;
int n = 5000000;
before = System.nanoTime();
Map<Integer, Integer> counts0 = computeCounts(z0, size, n);
after = System.nanoTime();
System.out.println(counts0+", duration "+(after-before)/1e6);
before = System.nanoTime();
Map<Integer, Integer> counts1 = computeCounts(z1, size, n);
after = System.nanoTime();
System.out.println(counts1+", duration "+(after-before)/1e6);
}
private static Map<Integer, Integer> computeCounts(
ZipfGenerator z, int size, int n)
{
Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>();
for (int i=1; i<=size; i++)
{
counts.put(i, 0);
}
for (int i=1; i<=n; i++)
{
int k = z.next();
counts.put(k, counts.get(k)+1);
}
return counts;
}
private static Map<Integer, Integer> computeCounts(
FastZipfGenerator z, int size, int n)
{
Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>();
for (int i=1; i<=size; i++)
{
counts.put(i, 0);
}
for (int i=1; i<=n; i++)
{
int k = z.next();
counts.put(k, counts.get(k)+1);
}
return counts;
}
}
// Based on http://diveintodata.org/tag/zipf/
class ZipfGenerator {
private Random rnd = new Random(0);
private int size;
private double skew;
private double bottom = 0;
public ZipfGenerator(int size, double skew) {
this.size = size;
this.skew = skew;
for(int i=1;i <=size; i++) {
this.bottom += (1/Math.pow(i, this.skew));
}
}
// the next() method returns an random rank id.
// The frequency of returned rank ids are follows Zipf distribution.
public int next() {
int rank;
double friquency = 0;
double dice;
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
while(!(dice < friquency)) {
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
}
return rank;
}
// This method returns a probability that the given rank occurs.
public double getProbability(int rank) {
return (1.0d / Math.pow(rank, this.skew)) / this.bottom;
}
}
class FastZipfGenerator
{
private Random random = new Random(0);
private NavigableMap<Double, Integer> map;
FastZipfGenerator(int size, double skew)
{
map = computeMap(size, skew);
}
private static NavigableMap<Double, Integer> computeMap(
int size, double skew)
{
NavigableMap<Double, Integer> map =
new TreeMap<Double, Integer>();
double div = 0;
for (int i = 1; i <= size; i++)
{
div += (1 / Math.pow(i, skew));
}
double sum = 0;
for(int i=1; i<=size; i++)
{
double p = (1.0d / Math.pow(i, skew)) / div;
sum += p;
map.put(sum, i-1);
}
return map;
}
public int next()
{
double value = random.nextDouble();
return map.ceilingEntry(value).getValue()+1;
}
}
It prints a random sample result (basically, a "histogram"), and some timing results. The timing results are something like
duration 6221.835052
duration 304.761282
showing that it will most likely be faster (even though this should not be considered as a "benchmark"...)
The source you obtained from https://diveintodata.org/2009/09/13/zipf-distribution-generator-in-java/ has some bugs.
Here are quick fixes. (1) In the constructor ZipfGeneator(int,double), make sure to compute up to size using equal sign.
public ZipfGenerator(int size, double skew) {
this.size = size;
this.skew = skew;
for(int i=1;i <= size; i++) {
this.bottom += (1/Math.pow(i, this.skew));
}
}
(2) replace
rank = rnd.nextInt(size);
with
rank = rnd.nextInt(size)+1;
Here is the complete source code.
import java.util.Random;
//credit: https://diveintodata.org/2009/09/13/zipf-distribution-generator-in-java/ [Online; December 2017]
public class ZipfGenerator {
private Random rnd = new Random(System.currentTimeMillis());
private int size;
private double skew;
private double bottom = 0;
public ZipfGenerator(int size, double skew) {
this.size = size;
this.skew = skew;
for(int i=1;i <= size; i++) {
this.bottom += (1/Math.pow(i, this.skew));
}
}
// the next() method returns an random rank id.
// The frequency of returned rank ids are follows Zipf distribution.
public int next() {
int rank;
double friquency = 0;
double dice;
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
while(!(dice < friquency)) {
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
}
return rank;
}
// This method returns a probability that the given rank occurs.
public double getProbability(int rank) {
return (1.0d / Math.pow(rank, this.skew)) / this.bottom;
}
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("usage: ./zipf size skew");
System.exit(-1);
}
ZipfGenerator zipf = new ZipfGenerator(Integer.valueOf(args[0]),
Double.valueOf(args[1]));
for(int i= 1;i <= 10; i++) {
System.out.println(i+" "+zipf.getProbability(i));
}
//use size = 10 and skew = 2 for testing below
int hist [] = new int [12];
for(int i=0;i<12;i++) {
hist[i] = 0;
}
System.out.println("Testing the probability distribution:");
int sum = 0;
for(int i= 1;i <= 1000000; i++) {
hist[zipf.next()]++;
}
for(int i=0;i<12;i++)
System.out.println(i+" "+hist[i]/1000000.0);
}
}
Result:
Probability distribution from the formula:
1 0.6452579827864142
2 0.16131449569660355
3 0.07169533142071269
4 0.04032862392415089
5 0.02581031931145657
6 0.017923832855178172
7 0.013168530260947229
8 0.010082155981037722
9 0.007966147935634743
10 0.006452579827864143
Testing the probability distribution from sampling:
0 0.0
1 0.645813
2 0.160766
3 0.071527
4 0.040346
5 0.026039
6 0.01801
7 0.013215
8 0.009953
9 0.007863
10 0.006468
11 0.0
Note, 0 and 11 has 0 probability as expected.
You were asking about speed, so I'm presenting a minor optimization. First of all, get rid of the repetitive stuff to see what it's all about:
public int next() {
while (true) {
int rank = rnd.nextInt(size);
if (rank == 0) return return rank;
double frequency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
double dice = rnd.nextDouble();
if (dice < frequency) return rank;
}
}
So far it should work exactly the same (unless I overlooked something). I moved the test of rank upwards as the following computation is useless in case it's zero. Now there's a single line which we can speed up a bit like
double frequency = Math.pow(rank, -this.skew) * inverseBottom;
Actually, this may slightly change the result due to round-off errors, but I doubt you should care. If rank was constant, you could turn pow into exp to make it faster, but it's not. For a small size, you could precompute a table of ln(rank) and use it like
double frequency = Math.exp(ln[rank] * -this.skew) * inverseBottom;
A better algorithm could surely give you more than this low-level optimizations.

Random variables in Java

My professor asked us to generate random variables between 0 and 0.5. I wrote this code:
public class Random_Number_Generator {
double randomGenerator() {
Random generator = new Random();
double num = generator.nextDouble() * (0.5 - 0);
return num;
}
}
But my professor is saying this code is generating random numbers not random variables. What could this mean?
Apparently I misread the post; the following should be read with that in mind.
In that code, num and generators are local variables. A random number (a value) is assigned to the variable called num using the Random object named by the generator variable. Finally, the value stored in the variable num is returned from the method.
In any case, generator.nextDouble() returns a value between [0,1) so to get a value between [0,0.5), just scale it by half: divide it by two or, as done, multiply it by a half.
The - 0 in the above code is silly, but "okay" because (0.5 - 0) == 0.5.
(Also, it is good to get into the practice of to creating one Random instance and re-using it .. although this issue is more obvious in .NET.)
Now, actual random variable is, as far as I know, a function that maps values to their probability. I don't think you're supposed to return a function, so I've scratched this: the closest thing to what I guess you're supposed to do:
import java.util.*;
import java.lang.*;
class RandomVar
{
TreeMap<Double, Integer> variables;
public RandomVar()
{
variables = new TreeMap<Double, Integer>();
int count = Main.RandGen.nextInt(15);
double probabilityLeft = 1.0;
for (int i = 0 ; i < count - 1; i++)
{
int toPut = Main.RandGen.nextInt(100);
while (variables.containsValue(toPut)) toPut = Main.RandGen.nextInt(100);
double prob = probabilityLeft * Main.RandGen.nextDouble();
variables.put(prob, toPut);
}
int toPut = Main.RandGen.nextInt(100);
while (variables.containsValue(toPut)) toPut = Main.RandGen.nextInt(100);
double prob = probabilityLeft;
variables.put(prob, toPut);
}
public int getValue()
{
double rand = Main.RandGen.nextDouble();
double sum = 0;
for (double prob : variables.keySet()) //keySet() is sorted ascending
{
if (prob >= rand)
return variables.get(prob);
}
return variables.get(variables.lastKey());
}
//Shows probabilities of values
public void test()
{
for (double key : variables.keySet())
System.out.println(key + " :: " + variables.get(key));
}
}
class Main
{
public static Random RandGen = new Random();
public static void main (String[] args)
{
RandomVar rv = new RandomVar();
rv.test();
System.out.println("------------------------------");
for (int i = 0; i < 40 ; i++)
System.out.print(rv.getValue() + ", ");
}
}
This is very lousy solution, basically a class which allows you to return values with a set (random) probability. I still don't know if this is what you professor wants though...
Try this code:
public static void main(String[] arg) {
System.out.print(Random());
}
public static double Random() {
double START = 0;
double END = 0.5;
Random random = new Random();
double token = RandomNumber(START, END, random);
return token;
}
public static double RandomNumber(double aStart, double aEnd, Random aRandom) {
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
// get the range, casting to long to avoid overflow problems
double range = aEnd - aStart;
// compute a fraction of the range, 0 <= frac < range
double fraction = (range * aRandom.nextDouble());
double randomNumber = (fraction + aStart);
return randomNumber;
}

How to give input for the double[] array in Java

How do I give input directly, when that function is invoked in another method, especially when that input is a double[] array?
public double dotPro1(double[] vectorA, double[] vectorB) {
double[] vecPro;
vecPro = new double[2];
vecPro[0] = vectorA[0]*vectorB[0];
vecPro[1] = vectorA[1]*vectorB[1];
return vecPro[0] + vecPro[1];
}
public double dotPro2(double[] length) {
double[] lenPro;
lenPro = new double[1];
lenPro[0] = length[0];
return lenPro[0];
}
public static double cosine(double a) {
double x = Math.cos(Math.toRadians(a));
/*Class c = Class.forName("NaiveStrategy");
Class methodTypes[] = new Class[3];
methodTypes[0] = Double.TYPE;
methodTypes[1] = Double.TYPE;
methodTypes[2] = Double.TYPE;
Method[] m = c.getMethods();*/
NaiveStrategy ns = new NaiveStrategy();
problem-->ns.dotPro1(vectorA[], vectorB[]);
problem-->ns.dotPro2(length[]);
return 0;
}
As you can also see my old coding I tried in another way to solve it, but it didn't worked. It's commented out above.
It's not at all clear what you mean - but certainly the syntax you've got at the moment won't work. If you're just looking to create an array at execution time, that's easy:
NaiveStrategy ns = new NaiveStrategy();
// Either using separate variables...
double[] first = { 0.5, 0.1, 10 };
double[] second { 5, 20.3, 1 };
double result1 = ns.dotPro1(first, second);
// Or inline...
double results2 = ns.dotPro2(new double[] { 50.2, 0.3 });
I can't tell why you've got commented-out reflection calls in your code though...
Also, as noted in comments, your methods seem to be creating arrays for no particular purpose. They could be rewritten as:
public double dotPro1(double[] vectorA, double[] vectorB) {
return vectorA[0] * vectorB[0] + vectorA[1] * vectorB[1];
}
public double dotPro2(double[] length) {
return length[0];
}
You said in one comment that you want random values. So use the Random class to generate them.
double[] list1 = new double[5];
double[] list2 = new double[5];
Random rand = new Random();
for (int i = 0; i < list1.length; i++) {
list1[i] = rand.nextDouble();
}
for (int i = 0; i < list2.length; i++) {
list2[i] = rand.nextDouble();
}
The new double[5]; will create an array of length 5. Change this number to the length you want.
Note: rand.nextdouble only gives a random number between 0 and 1. If you wish it to , say, be between 0 and 100 then use rand.nextDouble()*100
You can then just use
double result1 = ns.dotPro1(list1, list2);
When passing the array parameter do not put the square braces.
= ns.dotPro2(length);
= ns.dotPro1(vectorA, vectorB);
Sorry for the delay, as my net went off..... Ok back to the answer...
Here is the simplest way to do it...
double[] first = { 0.10, 0.15, 0.30 };
double[] second { 65, 44.3, 10.11 };
double result1 = ns.dotPro1(first, second);

Categories