1## Copyright (C) 2011-2021 L. Markowsky <lmarkov@users.sourceforge.net> 2## 3## This file is part of the fuzzy-logic-toolkit. 4## 5## The fuzzy-logic-toolkit is free software; you can redistribute it 6## and/or modify it under the terms of the GNU General Public License 7## as published by the Free Software Foundation; either version 3 of 8## the License, or (at your option) any later version. 9## 10## The fuzzy-logic-toolkit is distributed in the hope that it will be 11## useful, but WITHOUT ANY WARRANTY; without even the implied warranty 12## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13## General Public License for more details. 14## 15## You should have received a copy of the GNU General Public License 16## along with the fuzzy-logic-toolkit; see the file COPYING. If not, 17## see <http://www.gnu.org/licenses/>. 18 19## -*- texinfo -*- 20## @deftypefn {Function File} {@var{retval} =} hamacher_sum (@var{x}) 21## @deftypefnx {Function File} {@var{retval} =} hamacher_sum (@var{x}, @var{y}) 22## 23## Return the Hamacher sum of the input. 24## The Hamacher sum of two real scalars x and y is: 25## (x + y - 2 * x * y) / (1 - x * y) 26## 27## For one vector argument, apply the Hamacher sum to all of the elements 28## of the vector. (The Hamacher sum is associative.) For one 29## two-dimensional matrix argument, return a vector of the Hamacher sum 30## of each column. 31## 32## For two vectors or matrices of identical dimensions, or for one scalar and 33## one vector or matrix argument, return the pair-wise Hamacher sum. 34## 35## @seealso{algebraic_product, algebraic_sum, bounded_difference, bounded_sum, drastic_product, drastic_sum, einstein_product, einstein_sum, hamacher_product} 36## @end deftypefn 37 38## Author: L. Markowsky 39## Keywords: fuzzy-logic-toolkit fuzzy hamacher_sum 40## Directory: fuzzy-logic-toolkit/inst/ 41## Filename: hamacher_sum.m 42## Last-Modified: 20 Aug 2012 43 44function retval = hamacher_sum (x, y = 0) 45 if (nargin == 0 || nargin > 2 || 46 !is_real_matrix (x) || !is_real_matrix (y)) 47 argument_error 48 49 elseif (nargin == 1) 50 if (isvector (x)) 51 retval = vector_arg (x); 52 elseif (ndims (x) == 2) 53 retval = matrix_arg (x); 54 else 55 argument_error; 56 endif 57 58 elseif (nargin == 2) 59 if (isequal (size (x), size (y))) 60 retval = arrayfun (@scalar_args, x, y); 61 elseif (isscalar (x) && ismatrix (y)) 62 x = x * ones (size (y)); 63 retval = arrayfun (@scalar_args, x, y); 64 elseif (ismatrix (x) && isscalar (y)) 65 y = y * ones (size (x)); 66 retval = arrayfun (@scalar_args, x, y); 67 else 68 argument_error; 69 endif 70 endif 71endfunction 72 73function retval = scalar_args (x, y) 74 retval = (x + y - 2 * x * y) / (1 - x * y); 75endfunction 76 77function retval = vector_arg (real_vector) 78 x = 0; 79 for i = 1 : length (real_vector) 80 y = real_vector(i); 81 if (x == 1 && y == 1) 82 x = 1; 83 else 84 x = (x + y - 2 * x * y) / (1 - x * y); 85 endif 86 endfor 87 retval = x; 88endfunction 89 90function retval = matrix_arg (x) 91 num_cols = columns (x); 92 retval = zeros (1, num_cols); 93 for i = 1 : num_cols 94 retval(i) = vector_arg (x(:, i)); 95 endfor 96endfunction 97 98function argument_error 99 puts ("Type 'help hamacher_sum' for more information.\n"); 100 error ("invalid arguments to function hamacher_sum\n"); 101endfunction 102