GENOPS - Generalized Arithmetic Operators for Matlab

Copyright 1998-2001 by Douglas M. Schwarz.
Permission is granted for anyone to copy, use, or modify this software and accompanying documents for any uncommercial purposes, provided this copyright notice is retained, and note is made of any changes that have been made. This software and documents are distributed without any warranty, express or implied.

Modified and documented 1999-2003 by Aki Vehtari
See end for historical comments.
See also my M-file implementation GENOPSM.

Introduction

This text is modified from text in <http://www.frontiernet.net/~dmschwarz/genops.html>.

Recently (1999), in a discussion on the MATLAB newsgroup, comp.soft-sys.matlab, several MATLAB users suggested that it would be nice if MATLAB would allow you to write things like

 Y = X - mean(X) 
where X is an N-by-M matrix and you want to return each column minus its mean. Currently you have to write something like
 Y = X - repmat(mean(X),[N,1]) 
which requires extra memory to replicate the single row of mean(X), extra time to do it and isn't as clean looking. Generalizing this idea for N-dimensional arrays, what we want to do is compare the dimensions of the two operands and copy singleton dimensions in one to match the size of the other, without actually copying any data. Here is some examples for
 Z = X + Y
      
 size(X)   size(Y)   size(Z)    equivalent code
 [5 3]     [1 3]     [5 3]      Z = X + repmat(Y,[5 1])
 [4 5 1 7] [4 1 6 7] [4 5 6 7]  Z = repmat(X,[1 1 6 1]) + repmat(Y,[1 5 1 1])
 [4 5]     [4 5 6 7] [4 5 6 7]  Z = repmat(X,[1 1 6 7]) + Y
 [4 5]     [1 1 6 7] [4 5 6 7]  Z = repmat(X,[1 1 6 7]) + repmat(Y,[4 5 1 1]) 
Without changing the core code of MATLAB it is possible to implement this with MEX functions and operator overloading. Original implementation by Douglas M. Schwarz did this, but I think it would cause trouble. Instead I have installed the generalized operators as functions starting with letter `g'. So generalized versions of built-in functions plus, minus, times, rdivide, ldivide, power, eq, ne, lt, gt, le and ge are named as gplus, gminus, gtimes, grdivide, gldivide, gpower, geq, gne, glt, ggt, gle and gge. Above examples can then be written as
 Y = gminus(X, mean(X))
 Z = gplus(X, Y) 

Download

genops.tar.gz
genops.zip

Building MEX files

Build MEX file by entering the appropriate command at the MATLAB prompt (-D<name> option is equivalent to #define <name> in source file):

 mex genops.c -O -DPLUS_MEX -output gplus
 mex genops.c -O -DMINUS_MEX -output gminus
 mex genops.c -O -DTIMES_MEX -output gtimes
 mex genops.c -O -DRDIVIDE_MEX -output grdivide
 mex genops.c -O -DLDIVIDE_MEX -output gldivide
 mex genops.c -O -DPOWER_MEX -output gpower
 mex genops.c -O -DEQ_MEX -output geq
 mex genops.c -O -DNE_MEX -output gne
 mex genops.c -O -DLT_MEX -output glt
 mex genops.c -O -DGT_MEX -output ggt
 mex genops.c -O -DLE_MEX -output gle
 mex genops.c -O -DGE_MEX -output gge 

History of this release

Originally Douglas M. Schwarz had following copyright notice
 Copyright 1998-1999 by Douglas M. Schwarz. All rights reserved.
In 1999 I asked Doug about distributing genops with my modification and he answered:
 > I haven't thought about that at all. I released my software
 > for free, but it is copyrighted. I guess that means that you
 > can do whatever you want with it, but you must give me credit
 > and you can't make any money off of my work. When and if you
 > want to publish your version let me know and I'll look at the
 > GNU license. I certainly don't object to you distributing your
 > version of my code, again, as long as I get credit.
After this I have not been able to contact Doug anymore, so based on his reply I changed the copyright notice to following:
 Copyright 1998-1999 by Douglas M. Schwarz.
 Permission is granted for anyone to copy, use, or modify this
 software and accompanying documents for any uncommercial purposes,
 provided this copyright notice is retained, and note is made of
 any changes that have been made. This software and documents are
 distributed without any warranty, express or implied. 


Aki Vehtari
Last modified: 2006-02-28