Java tutorial
//package com.java2s; /* * Copyright (c) 2009-2011, Peter Abeles. All Rights Reserved. * * This file is part of Efficient Java Matrix Library (EJML). * * EJML is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * EJML is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with EJML. If not, see <http://www.gnu.org/licenses/>. */ public class Main { public static double computeTauAndDivide(int j, int numRows, double[] u, int startU, double max) { // compute the norm2 of the matrix, with each element // normalized by the max value to avoid overflow problems double tau = 0; // double div_max = 1.0/max; // if( Double.isInfinite(div_max)) { // more accurate for (int i = j; i < numRows; i++) { double d = u[startU + i] /= max; tau += d * d; } // } else { // // faster // for( int i = j; i < numRows; i++ ) { // double d = u[startU+i] *= div_max; // tau += d*d; // } // } tau = Math.sqrt(tau); if (u[startU + j] < 0) tau = -tau; return tau; } /** * Normalizes elements in 'u' by dividing by max and computes the norm2 of the normalized * array u. Adjust the sign of the returned value depending on the size of the first * element in 'u'. Normalization is done to avoid overflow. * * <pre> * for i=j:numRows * u[i] = u[i] / max * tau = tau + u[i]*u[i] * end * tau = sqrt(tau) * if( u[j] < 0 ) * tau = -tau; * </pre> * * @param j Element in 'u' that it starts at. * @param numRows Element in 'u' that it stops at. * @param u Array * @param max Max value in 'u' that is used to normalize it. * @return norm2 of 'u' */ public static double computeTauAndDivide(final int j, final int numRows, final double[] u, final double max) { double tau = 0; // double div_max = 1.0/max; // if( Double.isInfinite(div_max)) { for (int i = j; i < numRows; i++) { double d = u[i] /= max; tau += d * d; } // } else { // for( int i = j; i < numRows; i++ ) { // double d = u[i] *= div_max; // tau += d*d; // } // } tau = Math.sqrt(tau); if (u[j] < 0) tau = -tau; return tau; } }