# setMatrix C++API

**URL:** <https://discuss.ampl.com/t/setmatrix-c-api/695>\
**Category:** Support\
**Tags:** general, errors-and-messages, modeling\
**Created:** [August 25, 2023, 10:13pm UTC](https://discuss.ampl.com/t/setmatrix-c-api/695 "2023-08-25T22:13:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/matheus_diogenes_and/32/380_2.png) [@Matheus\_Diogenes\_And](https://discuss.ampl.com/u/Matheus_Diogenes_And)\
**Post date:** [August 25, 2023, 10:13pm UTC](https://discuss.ampl.com/t/setmatrix-c-api/695/1 "2023-08-25T22:13:42Z")

</div>

Hello.

I am trying to run the following piece of code, but I am having some errors when compiling.

```cpp
#include <iostream>
#include <string> 
#include <range/v3/all.hpp>
#include "ampl/ampl.h"

namespace rangesv3 = ::ranges;
using namespace std;
using namespace ampl;

int main(int argc, char **argv) {
    try {
        int n = 10;

        // Create an AMPL instance
        AMPL ampl;

        // sets
        ampl.eval("set N;");
        ampl.eval("param distance {N, N};");
        
        // N
        vector<string> N = rangesv3::views::iota(0, n)
            | rangesv3::views::transform([](const int& i) -> string { return to_string(i); })
            | rangesv3::to<vector<string>>();

        vector<const char *> N_ptr = N 
            | rangesv3::views::transform([](const string& i_str) -> const char * { return i_str.data(); })
            | rangesv3::to<vector<const char *>>();

        DataFrame N_df(1, "N");
        N_df.setColumn("N", N_ptr.data(), n);
        ampl.setData(N_df, "N");

        // distances
        vector<vector<double>> distances_double (n, vector<double>(n));
        for (int i = 0; i < n; ++i) 
            for (int j = 0; j < n; ++j) 
                distances_double[i][j] = i * j;

        vector<const double *> distances = distances_double 
             | rangesv3::views::transform([](const vector<double>& distance_line) -> const double * { 
                 return distance_line.data(); 
                 })
             | rangesv3::to<vector<const double *>>();

        DataFrame distance_df = DataFrame(2, StringArgs("N", "N", "distance"));
        distance_df.setMatrix(N_ptr.data(), N_ptr.data(), distances.data()); // error here
        ampl.setData(distance_df);

        return 0;
    } catch (const std::exception &e) {
        std::cout << e.what() << "\n";
        return 1;
    }
}

```

The error is the one below.

```sh
~/ampl_test/src/main.cpp: In function ‘int main(int, char**)’:
~/ampl_test/src/main.cpp:53:30: error: no matching function for call to ‘ampl::DataFrame::setMatrix(const char **, const char** , const double**)’
   53 | distance_df.setMatrix(N_ptr.data(), N_ptr.data(), distances.data());
      | ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

What is quite strange, because, if we take a look at the AMPL example `dietmodel.cc`, we have almost the same thing:

```cpp
double amounts[6][8] = {{60, 8, 8, 40, 15, 70, 25, 60},
                            {20, 0, 10, 40, 35, 30, 50, 20},
                            {10, 20, 15, 35, 15, 15, 25, 15},
                            {15, 20, 10, 10, 15, 15, 15, 10},
                            {928, 2180, 945, 278, 1182, 896, 1329, 1397}, {295, 770, 440, 430, 315, 400, 379, 450}};
    // Note the use of ampl::StringArgs to pass an array of strings
    df = ampl::DataFrame(2, ampl::StringArgs("NUTR", "FOOD", "amt"));
    df.setMatrix(nutrients, foods, amounts);
    ampl.setData(df);

```

Have anyone passed by this before?

Btw, the repository is [here](https://github.com/matheusdiogenesandrade/AMPL_cmake).

Thanks and regards.

---

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/matheus_diogenes_and/32/380_2.png) [@Matheus\_Diogenes\_And](https://discuss.ampl.com/u/Matheus_Diogenes_And)\
**Post date:** [August 27, 2023, 6:28pm UTC](https://discuss.ampl.com/t/setmatrix-c-api/695/2 "2023-08-27T18:28:24Z")

</div>

Hello.

After some time trying to solve the problem, I found the solution. Basically, I have to pass a flatted matrix, so if we define our matrix as:

```cpp
double * distances = (double *) malloc((n * n) * sizeof(double));

```

The problem is solved.

Thanks for the attention, and BR.
