From octave-sources-request at bevo dot che dot wisc dot edu Tue Apr 6 07:27:53 1999 Subject: File command 'hypot.m' From: Thomas Walter To: octave-sources at bevo dot che dot wisc dot edu Date: Tue, 6 Apr 1999 14:27:20 +0200 Hello, I have coded the function 'hypot()' as m-file. It computes 'c = sqrt (a^2 + b^2)' and tries its best to avoid overflow. For speed it first checks the arguments. If both are scalars then it computes the result with less overhead. For all other cases it uses the functions 'min()' and 'max()' with two arguments. NOTE: I realized this feature after looking at the sources of 'minmax.cc' because this is not described in the manual. I recently installed 'octave-2.0.13.97'. The source is below at the end. Bye Thomas ========== hypot.m start ============== ## Copyright (C) 1999 Thomas Walter ## ## This program is free software ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave 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 ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## ## Author: Thomas Walter ## Keywords: basic math ## Created: March 1999 function result = hypot (a, b) ## Compute the value of 'sqrt (a^2 + b^2)'. ## Try to avoid overflow. ## ## Usage: ## result = hypot (a, b) ## ## Input: ## a, b : if not scalar do it for each element ## ## Output: ## result : has the same type like the input ## Copyright (c) 1999 Thomas Walter ## $Id: hypot.m,v 1.8 1999/04/06 12:16:37 twalter Exp $ ## Sanity checks if (nargin != 2) usage ("result = hypot (a, b)"); endif ## Make all input positiv a = abs (a); b = abs (b); if (is_scalar (a) && is_scalar (b)) ## If both inputs are scalars, use a simpler version. ## In C/C++ you would have this for each supported type. if (a > b) ## a cannot be zero at this point result = a * sqrt (1 + (b / a)^2); else # !(a > b) if (b > 0) result = b * sqrt (1 + (a / b)^2); else result = a; endif # b > 0 endif # !(a > b) else # !(is_scalar (a) && is_scalar (b)) small_val = min (a, b); large_val = max (a, b); ## 1. step: only elements of 'large_val' greater zero to avoid division by zero. k = find (large_val > 0); if any (k) result (k) = large_val (k) * sqrt (1.0 + (small_val (k) / large_val (k)) ^ 2); endif ## 2. step: for 'large_val' equal zero. k = find (!(large_val > 0)); if any (k) result (k) = small_val (k); endif endif # !(is_scalar (a) && is_scalar (b)) endfunction ========== hypot.m end ============== -- "Beweiss" fuer die These: 'Alle ungeraden Zahlen sind prim.' 3, 5, 7, 11, alle sind ungerade und prim. 9 ist ist ein irrelevanter Messfehler. ==> Bingo! 8-)))))) ---------------------------------------------- Dipl. Phys. Thomas Walter Inst. f. Physiklische Chemie II Egerlandstr. 3 Tel.: ++9131-85 27326 / 27330 91058 Erlangen, Germany email: walter at pctc dot chemie dot uni-erlangen dot de