0

I have a matrix M, whose each element is dependent on a single variable t in a different quadratic form without the constant.

Just for example,

M[1,1] = 2t^2 + 4t
M[3,2] = t^2 - 5t
M[2,4] = -t^2 + 5t

The matrix is acquired after a series of calculation and each element, or say, the coefficients before t^2 and t, are figured out through a combination of a bunch of other functions.

I was wondering how I can store the matrix like a function M(t) so each time I can call the function to generate a matrix with different t.

Thanks!

UPDATE: The purpose is to get the smallest eigenvalue of the matrix given different t, so I was thinking I could generate a matrix each time and feed it to a eigenvalue solver to get the smallest eigenvalue for each t.

James LT
  • 733
  • 2
  • 12
  • 23

4 Answers4

1

From my limited understanding, what you are after is a function at specific point, for this, I would use a std::function<double(double)> (i.e. function that takes one double and returns a double (the result.) I guess that's what you are after? And each location of the matrix can be initialized with a lambda - e.g

// Assume my dumb matrix is a 2d vector
vector<vector<function<double(double)>>> matrix;

matrix[1][1] = [](double t) { return /* formula for: 2t^2 + 4t */ ; }

etc.

Nim
  • 33,299
  • 2
  • 62
  • 101
0

you should create a class name Matrix and this class has a constructor which has only one parameter t.

0xFFFFFFFF
  • 852
  • 5
  • 9
0
// quadratic polynomial
class QuadPoly {
public:
    QuadPoly(double _c2, double _c1, double _c0) : c2(_c2), c1(_c1), c0(_c0) {}
    // ... other constructors, assignment
    double operator()(double t) const {return (c2*t+c1)*t+c0;}
    // ... maybe operator+ operator-, if necessary
private:
    double c0, c1, c2;
};

// numerical Matrix
template<class T> class Matrix {
public:
    // ... constructors, destructor, assignment
    const T& operator()(int row, int col) const;
    T& operator()(int row, int col);
    // ... anything else
    friend class QuadPolyMatrix;
private:
    int nRows, nCols;
    int nVals; // nRows*nCols
    double* pVals; // or maybe double**
};

// matrix of quadratic polynomials
class QuadPolyMatrix : public Matrix<QuadPoly> {
public:
    Matrix<double> operator() (double t) const;
};
user31264
  • 6,557
  • 3
  • 26
  • 40
0

1. Functions hardcoded

If you really need a matrix object, you can make a class with t in constructor and function, which returns values.

class CMatrix {
public:
    CMatrix( double t_ ) : t( t_ ) {}

    double GetElement( int row, int col ) const {
        if( row == 3 && col == 2 ) {
            return t * t - t * 5;
        } else if( ) ...
    }

private:
    double t;
};

After that you can construct CMatrix mat( t );

And then get elements by mat.GetElement( 3, 2 );.

2. More versatile variant.

Make typedef of functions (they all have the same signature double f( double t )) and an array of pointers to functions. Then, you can make a SetFunction method, which sets a function in a given coordinate and GetElement calls function in given coordinate with parameter, which is stored in the field (like prev. example). But in this case you must set all needed functions before calling.

FedyuninV
  • 141
  • 10