0 Daumen
267 Aufrufe

Frage:

Implementieren Sie die folgenden Vektoroperationen für \( x, y \in \mathbb{R}^{n} \) und \( a \in \mathbb{R} \) als statische Methoden im beigefügten JAVA-Code. Führen Sie Laufzeitexperimente durch und visualisieren Sie die Ergebnisse mit dem beigefügten Latex-Skript.
1. \( y=a x+y \)
2. \( x^{\top} y \)
3. \( y=a y-x \)
4. \( y=a x \)

Da ich mich erst mit der Einführung in die Programmierung beschäftige bin ich mit dieser Numerik Aufgabe etwas überforderd - sprich was noch nicht, wie ich die Vektoroperationen implementieren soll bzw. kann.

Über eine Erklärung würde ich mich daher sehr freuen

Code:

Der beigefügte Java-Code lautet:

import java.io.FileWriter;

public class VectorMethods {

public static void main(String[] str) {

int maxPower=0,maxIter=0;

try {
maxPower=Integer.parseInt(str[0]);
maxIter=Integer.parseInt(str[1]);
}
catch(ArrayIndexOutOfBoundsException e) {
exception();
}
catch(NumberFormatException e) {
exception();
}

try {
FileWriter fileWriter=new FileWriter("vector_time");

int n=1000;

for(int l=0;l<maxPower;l++) {

long time_axpy_total=0,time_dot_total=0,time_axmy_total=0,time_scal_total=0;
double[] x=new double[n],y=new double[n];

for(int k=0;k<maxIter;k++) {
   
    for(int i=0;i<n;i++) {
x[i]=Math.random();
y[i]=Math.random();
}

long time_axpy=System.currentTimeMillis();
    axpy(Math.random(),x,y); 
    time_axpy_total+=System.currentTimeMillis()-time_axpy;

long time_dot=System.currentTimeMillis();
    dot(x,y); 
    time_dot_total+=System.currentTimeMillis()-time_dot;

long time_axmy=System.currentTimeMillis();
    aymx(Math.random(),y,x); 
    time_axmy_total+=System.currentTimeMillis()-time_axmy;

long time_scal=System.currentTimeMillis();
    scal(Math.random(),x,y); 
    time_scal_total+=System.currentTimeMillis()-time_scal;
}   

fileWriter.write(""+n+" "+time_axpy_total+" "+time_dot_total+" "+time_axmy_total+" "+time_scal_total+"\n");

n*=2;
}

fileWriter.close();
}
catch(java.io.IOException e) {
System.out.println("Some file problems occur.");
System.exit(0);
}
}

static void exception() {
System.out.println("Wrong parameters. Please use the following parameters:");
System.out.println(" maximum power of 2 (int)");
System.out.println(" maximum number of iterations (int)");
  System.exit(0);
}


// y=ax+y
static void axpy(double a,double[] x,double[] y) {

}

// <x,y>
static double dot(double[] x,double[] y) {
return 0;
}

// y=ay-x;
static void aymx(double a,double[] y,double[] x) {

}

// y=ax
static void scal(double a,double[] x,double[] y) {

}
}

Vielen Dank für Hilfe im Voraus

Avatar von

1 Antwort

0 Daumen
 
Beste Antwort
static void axpy(double a,double[] x,double[] y) {
for (int i = 0; i < y.length; ++i) {
y[i] = ...
}
}
static double dot(double[] x,double[] y) {
double result = 0;
for (int i = 0; i < y.length, ++i) {
result = ...
}
return result;
}
Avatar von 5,6 k

Dankeschön für deine Hilfe

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community