3

多次元配列を操作するプログラムを作りたい、新しいコンセプトを学びたい…。

私はそれがいくつかの方法を持っている可能性があると思います...

public int get(int row, int column)
//  a method to get the value in row/column
//  keep in mind columns/rows start at 0


public void set(int row, int column, int value)
// a method to set the matrix element to the value
// again keep in mind columns/rows start at 0


public void negate()
// method that negates through each element

public void add(Matrix m)
// adds the matrix m to this

これが正しいアプローチであるかどうか誰かに教えてもらえますか?どうすればこれを行うことができますか?前もって感謝します

4

4 に答える 4

2

あなたのアプローチは一般的に賢明に見えます。

考慮すべきいくつかのこと:

  • int数学行列の基本的なデータ型にはあまり適していません。たとえば、通常、行列の正確な逆int行列を取得することはできません。代わりに使用したくないと思いますdoubleか?
  • このようなベクトル/行列を不変にすることをお勧めします。その場合add、、は新しい行列を返す必要がありますsetnegate可変性を支持する唯一の本当の議論はパフォーマンスです(しかしその場合...あなたはあなた自身のものを転がすのではなく既存のライブラリーを使うべきです!)

このような(そしてさらに多くの)ことを行う既存のライブラリを使用または研究したい場合は、私のかなり広範なベクトル/行列数学ライブラリに興味があるかもしれません:

Vectorzは、高速な数値計算のためのプリミティブ配列に特に焦点を当ててdoubleいますが、同じ原則があらゆるタイプの配列/行列に適用されます。

于 2013-02-26T04:32:14.520 に答える
1

Take a look at this implementation and analyze it, if this is for homework then 'clipboard inheritance' won't do you good if you don't know whats going on.

    public class Matrix
{
    private double[][] matrixData;

    private int col;

    private int row;

    /**
     * Create matrix of zero size
     */
    public Matrix()
    {
        this(0);
    }

    public Matrix(double[][] matrixData)
    {
        this.matrixData = matrixData;

        this.row = matrixData.length;
        this.col = matrixData[0].length;
    }

    public Matrix(double[] matrixData)
    {
        this.row = matrixData.length;
        for (int i = 0, size = matrixData.length; i < size; i++)
        {
            this.matrixData[0][i] = matrixData[i];
        }
    }

    public Matrix(int size)
    {
        this.matrixData = new double[size][size];
        this.col = size;
        this.row = size;
    }

    public Matrix(int row, int col)
    {
        matrixData = new double[row][col];
        this.row = row;
        this.col = col;
    }

    /**
     * Static method which creates a matrix with a single column.
     * 
     * @param input
     * @return
     */
    public static Matrix createColumnMatrix(final double input[])
    {
        double result[][] = new double[input.length][1];
        for (int i = 0; i < input.length; i++)
            result[i][0] = input[i];

        return new Matrix(result);
    }

    /**
     * Static method which creates a matrix with a single row.
     * 
     * @param input
     * @return
     */
    public static Matrix createRowMatrix(final double input[])
    {
        double result[][] = new double[1][input.length];
        for (int i = 0, size = input.length; i < size; i++)
            result[0][i] = input[i];

        return new Matrix(result);
    }

    /**
     * Convert the matrix into a one dimentional matrix
     * 
     * @return
     */
    public double[] toPackedArray()
    {
        int size = row * col;
        int index = 0;
        double[] results = new double[size];
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                results[index++] = matrixData[i][j];

        return results;
    }

    /**
     * Adds the specified value to given cell in the matrix.
     * 
     * @param row
     * @param col
     * @param value
     */
    public void add(final int row, final int col, final double value)
    {
        matrixData[row][col] += value;
    }

    /**
     * Sets every cell in a matrix to zero.
     */
    public void clear()
    {
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                matrixData[i][j] = 0;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException
    {
        return new Matrix(matrixData);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (!(obj instanceof Matrix))
        {
            return false;
        }
        Matrix that = (Matrix)obj;
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
            {
                if (matrixData[i][j] != that.get(i,j))
                    return false;
            }

        return true;
    }

    public void print()
    {
        for (int i = 0; i < matrixData.length; i++)
        {
            for (int j = 0; j < matrixData[i].length; j++)
            {
                System.out.print(matrixData[i][j] + ", ");
            }
            System.out.println();
        }
    }

    /**
     * Gets one column of a matrix object as a new matrix object.
     * 
     * @param col
     * @return
     */
    public Matrix getCol(final int col)
    {
        double[] results = new double[matrixData[col].length];
        for (int i = 0; i < row; i++)
            results[i] = matrixData[i][col];

        return Matrix.createColumnMatrix(results);
    }

    /**
     * Gets one row of a matrix object as a new matrix object.
     * 
     * @param row
     * @return
     */
    public Matrix getRow(final int row)
    {
        double[] results = new double[this.row];
        for (int i = 0; i < col; i++)
            results[i] = matrixData[row][i];

        return Matrix.createRowMatrix(results);
    }

    /**
     * Returns the sum of every cell in a matrix object.
     * 
     * @return
     */
    public double sum()
    {
        double total = 0;
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
            {
                total += matrixData[i][j];
            }
        return total;
    }

    /**
     * Determines if every cell in a matrix object is zero.
     * 
     * @return
     */
    public boolean isZero()
    {
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
            {
                if (matrixData[i][j] != 0)
                    return false;
            }

        return true;
    }

    /**
     * Get given value at a specific location
     * 
     * @param row
     * @param col
     * @return
     */
    public double get(int row, int col)
    {
        return matrixData[row][col];
    }

    public int getCols()
    {
        return col;
    }

    public int getRows()
    {
        return row;
    }

    public static Matrix identity(final int size)
    {
        final Matrix result = new Matrix(size,size);

        for (int i = 0; i < size; i++)
        {
            result.set(i,i,1);
        }
        return result;
    }

    /**
     * Set given row and column using 0 based indexes
     * 
     * @param row
     * @param col
     * @param value
     */
    public void set(int row, int col, double value)
    {
        matrixData[row][col] = value;
    }

    /**
     * Get the copy of the current matrix
     * 
     * @return
     */
    public double[][] getRawMatrix()
    {
        double[][] copy = new double[matrixData.length][];
        for (int i = 0; i < matrixData.length; i++)
        {
            copy[i] = new double[matrixData[i].length];
            for (int j = 0; j < matrixData[i].length; j++)
                copy[i][j] = matrixData[i][j];
        }
        return copy;
    }

    public static void dump(double[] arr)
    {
        for (int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i] + ", ");
        }
    }

    public void dump()
    {
        dump(System.out);
    }

    public void dump(PrintStream ps)
    {
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                ps.print(matrixData[i][j] + ", ");
            }
            ps.println();
        }
        ps.println();
    }

    public void add(Matrix contribution)
    {
        // TODO Auto-generated method stub

    }
于 2013-02-26T04:28:52.073 に答える
1

これが正しいアプローチであるかどうか誰かに教えてもらえますか?どうすればこれを行うことができますか?

そのとおり。

ただし、慣例として、コメントはメソッド宣言の上に置く必要があります。メソッドヘッダーと呼ばれます。

例えば:

//  a method to get the value in row/column
//  keep in mind columns/rows start at 0
public int get(int row, int column)
于 2013-02-26T04:31:28.003 に答える
0

あなたのインターフェース(すなわちあなたの計画された方法)は非常に合理的なようです。

一般的な多次元配列操作に関しては、これについて利用できるチュートリアルがたくさんあります。たとえば、ここここここ、ここ、ここ、ここなどです

Aaandはじめに、簡単なコード例:

素晴らしい多次元配列の作成

int[][] multi = new int[2][3]; // this mda has two rows and three columns

それらのrad要素へのアクセス

int el = multi[0][1]; // el is in the first row, second column

ボスのようにそれらを繰り返す

for (int rowNum = 0; rowNum < multi.length; rowNum++) {
    for (int colNum = 0; colNum < multi[rowNum].length; colNum++) {
        int currentElement = multi[rowNum][colNum];
    }
}
于 2013-02-26T04:37:58.220 に答える