File: drastic_product.m

package info (click to toggle)
octave-fuzzy-logic-toolkit 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,024 kB
  • sloc: makefile: 147
file content (132 lines) | stat: -rw-r--r-- 4,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
## Copyright (C) 2011-2025 L. Markowsky <lmarkowsky@gmail.com>
##
## This file is part of the fuzzy-logic-toolkit.
##
## The fuzzy-logic-toolkit 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 3 of
## the License, or (at your option) any later version.
##
## The fuzzy-logic-toolkit 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 the fuzzy-logic-toolkit; see the file COPYING.  If not,
## see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} drastic_product (@var{x})
## @deftypefnx {Function File} {@var{retval} =} drastic_product (@var{x}, @var{y})
##
## Return the drastic product of the input.
##
## The drastic product of two real scalars x and y is:
##
## @verbatim
##     min (x, y)     if max (x, y) == 1
##     0              otherwise
## @end verbatim
##
## For one vector argument, apply the drastic product to all of the elements
## of the vector. (The drastic product is associative.) For one
## two-dimensional matrix argument, return a vector of the drastic product
## of each column.
##
## For two vectors or matrices of identical dimensions, or for one scalar and
## one vector or matrix argument, return the pairwise drastic product.
##
## @seealso{algebraic_product, algebraic_sum, bounded_difference, bounded_sum, drastic_sum, einstein_product, einstein_sum, hamacher_product, hamacher_sum}
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fuzzy drastic_product
## Directory:     fuzzy-logic-toolkit/inst/
## Filename:      drastic_product.m
## Last-Modified: 26 Jul 2024

function retval = drastic_product (x, y = 0)
  if (nargin == 0 || nargin > 2 ||
      !is_real_matrix (x) || !is_real_matrix (y))
    error ("invalid arguments to function drastic_product\n");

  elseif (nargin == 1)
    if (isvector (x))
      retval = vector_arg (x);
    elseif (ndims (x) == 2)
      retval = matrix_arg (x);
    else
      error ("invalid arguments to function drastic_product\n");
    endif

  elseif (nargin == 2)
    if (isequal (size (x), size (y)))
      retval = arrayfun (@scalar_args, x, y);
    elseif (isscalar (x) && ismatrix (y))
      x = x * ones (size (y));
      retval = arrayfun (@scalar_args, x, y);
    elseif (ismatrix (x) && isscalar (y))
      y = y * ones (size (x));
      retval = arrayfun (@scalar_args, x, y);
    else
      error ("invalid arguments to function drastic_product\n");
    endif
  endif
endfunction

function retval = scalar_args (x, y)
  if (max (x, y) == 1)
    retval = min (x, y);
  else
    retval = 0;
  endif
endfunction

function retval = vector_arg (x)
  if (isempty (x))
    retval = 1;
  elseif (max (x) == 1)
    retval = min (x);
  else
    retval = 0;
  endif
endfunction

function retval = matrix_arg (x)
  num_cols = columns (x);
  retval = zeros (1, num_cols);
  for i = 1 : num_cols
    retval(i) = vector_arg (x(:, i));
  endfor
endfunction

%!test
%! x = [0.5 0.2];
%! z = drastic_product(x);
%! assert(z, 0);

%!test
%! x = [0.5 0.2 0.3 1];
%! y = [1 0 0.2 0.3];
%! z = drastic_product(x, y);
%! assert(z, [0.5 0 0 0.3]);

## Test input validation
%!error <invalid arguments to function drastic_product>
%! drastic_product()
%!error <drastic_product: function called with too many inputs>
%! drastic_product(1, 2, 3)
%!error <invalid arguments to function drastic_product>
%! drastic_product(2j)
%!error <invalid arguments to function drastic_product>
%! drastic_product(1, 2j)
%!error <invalid arguments to function drastic_product>
%! drastic_product([1 2j])
%!error <invalid arguments to function drastic_product>
%! drastic_product([1 2], [1 2 3])
%!error <invalid arguments to function drastic_product>
%! drastic_product([1 2], [1 2; 3 4])
%!error <invalid arguments to function drastic_product>
%! drastic_product(0:100, [])