EasyLink
EasyLink is a C++ software development kit for developing blocks for SIMULINK (S-functions)
/Users/guillaume.laurent/Documents/Prog/easylinkEigen/easylink/mexArrayProductWithEigen.cpp
1 /*
2  * This file illustrates how to use Eigen within a C++ MEX-File and EasyLink.
3  *
4  * The function multiplies an input scalar times an input NxM ArrayXd and
5  * outputs a NxM ArrayXd.
6  *
7  * The calling syntax is:
8  *
9  * outArray = mexArrayProductWithEigen(multiplier, inArray)
10  *
11  * To compile this C++ MEX-File, enter the following command in MATLAB:
12  *
13  * >>make mexArrayProductWithEigen.cpp
14  *
15  */
16 
17 //------------------------------------------------------------------------------
18 
19 #include "EasyLink.h"
20 
21 #include <Eigen/Dense>
22 
23 #define EIGEN_INPUT_MATRIX(type, name, port) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getInputData(port), getInputNRows(port), getInputNCols(port))
24 #define EIGEN_INPUT_ARRAY(type, name, port) Eigen::Map<Eigen::Array<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getInputData(port), getInputNRows(port), getInputNCols(port))
25 #define EIGEN_OUTPUT_MATRIX(type, name, port) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getOutputData(port), getOutputNRows(port), getOutputNCols(port))
26 #define EIGEN_OUTPUT_ARRAY(type, name, port) Eigen::Map<Eigen::Array<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getOutputData(port), getOutputNRows(port), getOutputNCols(port))
27 
28 //------------------------------------------------------------------------------
29 
30 class Function : public BaseFunction {
31 public:
32 
33  // Specifies the number and the sizes of the input ports of the function
34  // (right-side arguments)
35 
36  static void initializeInputPortSizes() {
38  setInputPort(0, 1, 1, mxDOUBLE_CLASS);
39  setInputPort(1, -1, -1, mxDOUBLE_CLASS);
40  }
41 
42  // Specifies the number and the sizes of the output ports of the function
43  // (right-side arguments)
44 
45  static void initializeOutputPortSizes() {
47  setOutputPort(0, getInputNRows(1), getInputNCols(1), mxDOUBLE_CLASS);
48  }
49 
50  // Calculates the function using Eigen
51 
52  static void outputs() {
53  double multiplier = getInputDouble(0);
54  EIGEN_INPUT_ARRAY(double, inArray, 1);
55  EIGEN_OUTPUT_ARRAY(double, outArray, 0);
56  outArray = inArray*multiplier;
57  }
58 
59 };
60 
61 //------------------------------------------------------------------------------
62 
63 #include "mexDefinitions.h"
64 
65 //------------------------------------------------------------------------------
static void setInputPort(int port, int nRows, int nCols, mxClassID type=mxDOUBLE_CLASS, mxComplexity complexFlag=mxREAL)
Definition: BaseFunction.h:44
static void setOutputPort(int port, int nRows, int nCols, mxClassID type=mxDOUBLE_CLASS, mxComplexity complexFlag=mxREAL)
Definition: BaseFunction.h:88
static void initializeInputPortSizes()
Definition: BaseFunction.h:72
static int getInputNCols(int port)
Definition: BaseFunction.h:192
static void outputs()
Definition: BaseFunction.h:117
static double getInputDouble(int port)
Definition: BaseFunction.h:123
static void initializeOutputPortSizes()
Definition: BaseFunction.h:106
static void setInputPortsCount(int portsCount)
Definition: BaseFunction.h:33
static void setOutputPortsCount(int portsCount)
Definition: BaseFunction.h:79
static int getInputNRows(int port)
Definition: BaseFunction.h:185

Copyright (c) 2014 FEMTO-ST / ENSMM / UFC / UTBM, Besançon, France. Generated by Doxygen.