aboutsummaryrefslogtreecommitdiff
path: root/src/mtx_minmax.c
blob: c2d128e56dd8bb03e9f67356cd06570caa000d20 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 *  iemmatrix
 *
 *  objects for manipulating simple matrices
 *  mostly refering to matlab/octave matrix functions
 *
 * Copyright (c) 2005, Franz Zotter
 * IEM, Graz, Austria
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 */


#include "iemmatrix.h"

static t_class *mtx_minmax_class;
static t_symbol *row_sym;
static t_symbol *col_sym;
static t_symbol *col_sym2;

typedef struct _MTXminmax_ MTXminmax;
struct _MTXminmax_
{
  t_object x_obj;
  int size;
  int outsize;
  int mode;
  int operator_minimum; /* 1 if we are [mtx_min], 0 if we are [mtx_max] */

  t_outlet *min_outlet;
  t_outlet *max_outlet;

  t_atom *minlist_out;
  t_atom *maxlist_out;
};

static void deleteMTXMinMax (MTXminmax *mtx_minmax_obj) 
{
  if (mtx_minmax_obj->maxlist_out)
    freebytes (mtx_minmax_obj->maxlist_out, sizeof(t_atom)*(mtx_minmax_obj->size));
  if (mtx_minmax_obj->minlist_out)
    freebytes (mtx_minmax_obj->minlist_out, sizeof(t_atom)*(mtx_minmax_obj->size));
}

static void mTXSetMinMaxMode (MTXminmax *mtx_minmax_obj, t_symbol *m_sym)
{
  int mode=0;
  if(gensym("row")==m_sym)
    mode=1;
  else if((gensym("col")==m_sym) || (gensym("column")==m_sym) || (gensym(":")==m_sym))
    mode=2;

  mtx_minmax_obj->mode = mode;
}

static void *newMTXMinMax (t_symbol *s)
{
  MTXminmax *mtx_minmax_obj = (MTXminmax *) pd_new (mtx_minmax_class);

  mtx_minmax_obj->mode=0;

  mtx_minmax_obj->operator_minimum = 1;

  mtx_minmax_obj->min_outlet = outlet_new (&mtx_minmax_obj->x_obj, gensym("matrix"));
  mtx_minmax_obj->max_outlet = outlet_new (&mtx_minmax_obj->x_obj, gensym("matrix"));

  if((NULL!=s)&&(&s_!=s)&&(NULL!=s->s_name))
    mTXSetMinMaxMode (mtx_minmax_obj, s);

  return ((void *) mtx_minmax_obj);
} 

static void mTXMinMaxBang (MTXminmax *mtx_minmax_obj)
{
  if (mtx_minmax_obj->maxlist_out) 
    outlet_list(mtx_minmax_obj->max_outlet, gensym("list"),
                    mtx_minmax_obj->outsize, mtx_minmax_obj->maxlist_out);
  if (mtx_minmax_obj->minlist_out) 
    outlet_list(mtx_minmax_obj->min_outlet, gensym("list"), 
                    mtx_minmax_obj->outsize, mtx_minmax_obj->minlist_out);
}

static void minmaxList (int n, t_atom *x, t_float*min, t_float*max)
{
  t_float min_=atom_getfloat(x);
  t_float max_=min_;
  t_float f;
  for (;n--;x++) {
    f = atom_getfloat(x);
    min_ = (min_ < f)?min_:f;
    max_ = (max_ > f)?max_:f;
  }
  *max=max_;
  *min=min_;
}

static void minmaxListStep (int n, const int step, t_atom *x, t_float*min, t_float*max)
{
  t_float min_=atom_getfloat(x);
  t_float max_=min_;
  t_float f;
  for (;n--;x+=step) {
    f = atom_getfloat(x);
    min_ = (min_ < f)?min_:f;
    max_ = (max_ > f)?max_:f;
  }
  *max=max_;
  *min=min_;
}

static void minmaxListColumns (const int rows, const int columns, t_atom *x, 
                               t_atom *ap_min, t_atom *ap_max)
{
  int count;
  t_float min, max;
  for (count=0; count < columns; count++, x++, ap_min++, ap_max++) {
    minmaxListStep (rows, columns, x, &min, &max);
    SETFLOAT(ap_min,min);
    SETFLOAT(ap_max,max);
  }
}
static void minmaxListRows (int rows, int columns, t_atom *x, 
                            t_atom *ap_min, t_atom*ap_max)
{
  int count;
  t_float min, max;
  for (count=0; count < rows; count++, x+=columns, ap_min++, ap_max++) {
    minmaxList (columns, x, &min, &max);
    SETFLOAT(ap_min, min);
    SETFLOAT(ap_max,max);
  }
}
static void mTXMinMaxMatrix (MTXminmax *mtx_minmax_obj, t_symbol *s, 
                             int argc, t_atom *argv)
{
  int rows = atom_getint (argv++);
  int columns = atom_getint (argv++);
  int size = rows * columns;
  t_atom *maxlist_out = mtx_minmax_obj->maxlist_out;
  t_atom *minlist_out = mtx_minmax_obj->minlist_out;
  int elements_out;
  
  /* size check */
  if (!size) {
    post("mtx_minmax: invalid dimensions");
    return;
  }
  else if ((argc-2)<size) {
    post("mtx_minmax: sparse matrix not yet supported: use \"mtx_check\"");
    return;
  }
   
  if (size != mtx_minmax_obj->size) {
    if (!minlist_out)
      minlist_out = (t_atom *) getbytes (sizeof (t_atom) * size);
    else
      minlist_out = (t_atom *) resizebytes (minlist_out,
                                            sizeof (t_atom) * (mtx_minmax_obj->size),
                                            sizeof (t_atom) * size);
    if (!maxlist_out)
      maxlist_out = (t_atom *) getbytes (sizeof (t_atom) * size);
    else
      maxlist_out = (t_atom *) resizebytes (maxlist_out,
                                            sizeof (t_atom) * (mtx_minmax_obj->size),
                                            sizeof (t_atom) * size);
  }

  mtx_minmax_obj->size = size;
  mtx_minmax_obj->minlist_out = minlist_out;
  mtx_minmax_obj->maxlist_out = maxlist_out;
  
  /* main part */
  
  switch(mtx_minmax_obj->mode){
  case 1:
    elements_out = rows;
    minmaxListRows (rows, columns, argv, minlist_out, maxlist_out);
    break;
  case 2:
    elements_out = columns;
    minmaxListColumns (rows, columns, argv, minlist_out, maxlist_out);
    break;
  default:
    elements_out = 1;
    minmaxListRows (1, size, argv, minlist_out, maxlist_out);
  }
  mtx_minmax_obj->outsize = elements_out;
  maxlist_out = mtx_minmax_obj->maxlist_out;
  minlist_out = mtx_minmax_obj->minlist_out;
  
  mTXMinMaxBang(mtx_minmax_obj);
}

void mtx_minmax_setup (void)
{
  mtx_minmax_class = class_new (
                                gensym("mtx_minmax"),
                                (t_newmethod) newMTXMinMax,
                                (t_method) deleteMTXMinMax,
                                sizeof (MTXminmax),
                                CLASS_DEFAULT, A_DEFSYM, 0);

  class_addbang (mtx_minmax_class, (t_method) mTXMinMaxBang);
  class_addmethod (mtx_minmax_class, (t_method) mTXMinMaxMatrix, gensym("matrix"), A_GIMME,0);
  class_addmethod (mtx_minmax_class, (t_method) mTXSetMinMaxMode, gensym("mode"), A_DEFSYMBOL ,0);



  row_sym = gensym("row");
  col_sym = gensym("col");
  col_sym2 = gensym("column");
}

void iemtx_minmax_setup(void){
  mtx_minmax_setup();
}