# qualpal::Matrix

C++ · 4.1.0

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

```cpp
template<typename T> class qualpal::Matrix
```

Dynamic matrix class with runtime-determined dimensions. 

<a id="documentation-template-parameters"></a>

### Template parameters

**`T`**

Element type (typically double or float).

 Stores elements in column-major order (Fortran-style). Provides element access, transposition, and dimension queries.

Example: 

```cpp
Matrix<double> mat(2, 3); // 2 rows, 3 columns
mat(0, 1) = 5.0;          // Set element at row 0, column 1
double v = mat(1, 2);     // Get element at row 1, column 2
```

<a id="documentation-see-also"></a>

### See also

[FixedMatrix](<b242ab75f9cc5ae93b49a8cdab859487f3bf5aa6ff09b91c7ab5e23609c12f63>) for compile-time sized matrices 

<a id="cpp-methods"></a>

## Methods

<a id="cpp-df7f5f0bce57bb9d8c83163ef523c17abf1519de67ee8a87b1f216b431afaad5"></a>

### `Matrix`

<a id="cpp-85dbcac748985815a88e5a072482efe9604914b2a4d5e56fbe616a8c6e762721"></a>

#### `Matrix`

```cpp
inline Matrix()
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Default constructor creates an empty matrix (0x0). 

<a id="cpp-5d1ca2085dd78447f774e30954543f501e6bb37a79f71b23f91beef659993e04"></a>

#### `Matrix`

```cpp
inline Matrix(std::size_t rows, std::size_t cols)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Construct matrix with specified dimensions. 

<a id="cpp-5d1ca2085dd78447f774e30954543f501e6bb37a79f71b23f91beef659993e04-documentation-parameters"></a>

###### Parameters

**`rows`**

Number of rows 

**`cols`**

Number of columns 

<a id="cpp-1b0a0095be297087fd8895baf0bc365361abe8f97d585081fd90dc5d177b4523"></a>

#### `Matrix`

```cpp
inline Matrix(std::size_t rows, std::size_t cols, const std::vector<T> & data)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Construct matrix with specified dimensions and data. 

<a id="cpp-1b0a0095be297087fd8895baf0bc365361abe8f97d585081fd90dc5d177b4523-documentation-parameters"></a>

###### Parameters

**`rows`**

Number of rows 

**`cols`**

Number of columns 

**`data`**

Vector containing matrix elements in column-major order 

<a id="cpp-f2cb97013ecc11bd61dd21368b4d608964783b205b9fde6ce111736752ff2749"></a>

### `ncol`

```cpp
inline std::size_t ncol() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Get number of columns. 

<a id="cpp-af87e66d8d7c9d2fe6de1ebe10edd4d305d1faaadd23dfa91622145ab26bb17a"></a>

### `nrow`

```cpp
inline std::size_t nrow() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Get number of rows. 

<a id="cpp-1019b511eddf73bb6dda8ca6c0dac0b0fe30da351ca883f0aec1084fc5c32fbc"></a>

### `operator()`

<a id="cpp-efab8e31a0c7517f9b2605b995ac54c7523de253c6ae0b239e56cc436456587f"></a>

#### `operator()`

```cpp
inline T & operator()(std::size_t row, std::size_t col)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Access matrix element (mutable). 

<a id="cpp-efab8e31a0c7517f9b2605b995ac54c7523de253c6ae0b239e56cc436456587f-documentation-parameters"></a>

###### Parameters

**`row`**

Row index (0-based). 

**`col`**

Column index (0-based). 

 

<a id="cpp-efab8e31a0c7517f9b2605b995ac54c7523de253c6ae0b239e56cc436456587f-documentation-returns"></a>

###### Returns

Reference to element at (row, col). 

 

<a id="cpp-efab8e31a0c7517f9b2605b995ac54c7523de253c6ae0b239e56cc436456587f-documentation-note"></a>

###### Note

Elements are stored in column-major order: data\[col \* nrow + row\]. 

 

<a id="cpp-efab8e31a0c7517f9b2605b995ac54c7523de253c6ae0b239e56cc436456587f-documentation-throws"></a>

###### Throws

**`std::out_of_range`**

if indices are out of bounds (in debug builds). 

<a id="cpp-1f4fd8b8dc73354a3e69d7b2cb1cd45482c56b256b282e4ab245950d97f7f37b"></a>

#### `operator()`

```cpp
inline const T & operator()(std::size_t row, std::size_t col) const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Access matrix element (const). 

<a id="cpp-1f4fd8b8dc73354a3e69d7b2cb1cd45482c56b256b282e4ab245950d97f7f37b-documentation-parameters"></a>

###### Parameters

**`row`**

Row index (0-based). 

**`col`**

Column index (0-based). 

 

<a id="cpp-1f4fd8b8dc73354a3e69d7b2cb1cd45482c56b256b282e4ab245950d97f7f37b-documentation-returns"></a>

###### Returns

Const reference to element at (row, col). 

 

<a id="cpp-1f4fd8b8dc73354a3e69d7b2cb1cd45482c56b256b282e4ab245950d97f7f37b-documentation-note"></a>

###### Note

Elements are stored in column-major order: data\[col \* nrow + row\]. 

 

<a id="cpp-1f4fd8b8dc73354a3e69d7b2cb1cd45482c56b256b282e4ab245950d97f7f37b-documentation-throws"></a>

###### Throws

**`std::out_of_range`**

if indices are out of bounds (in debug builds). 

<a id="cpp-b98cac10f9bbc997ff4de65df5a1c42f64bd29a0f84bee78d411603d0f4dfff4"></a>

### `transpose`

```cpp
inline Matrix<T> transpose() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/matrix.h>)

Create transpose of this matrix. 

<a id="cpp-b98cac10f9bbc997ff4de65df5a1c42f64bd29a0f84bee78d411603d0f4dfff4-documentation-returns"></a>

##### Returns

New matrix that is the transpose of this matrix
