I'm using Octave in cpp code. I call Octave functions using feval. The code is-
octave_value_list out = feval (pstData[0], in, 1);
Which function do I have to use to determine the data type and size of each element in the list out? I have to check if the element is scalar/2D matrix/hypermatrix, the data type- complex/real/int/float/double/bool/string and the size of the matrix.
From the doc of octave_value_list, I'd say you can access its elements through the operator(). This way, you will get an octave_value, a type that offers all the functions you need, like is_float_type(), is_double_type(), etc..
Example (not tested)
octave_value& v = out(2); // access the value with id 2
bool is_double = v.is_double_type; // check wether out(2) is a double
Related
I am trying to "push" a big float into a Tuple. But get following error:
# where test() is a function with big floats as values
store = Tuple{Any, Any}][]
for i in 1:10
push!(store, test(i))
end
store
The error message mentions convert() as a solution, but I am not sure how to convert test().
You cannot push BigFloat into a container that accepts only Tuples. Your container has to accept BigFloats instead, so initialize it with:
store = BigFloat[]
Also note that you could have just written:
store = test.(1:10)
I have a tuple function that returns a tuple of the form
<node*,int>
Is there a way to store 2 values at once without creating another tuple. I know we can do
n,score=tuplefunct(abc);
in python. But if I want to store both return values in c++ without making another tuple i need to call twice
n=get<0>(tuplefunct(abc);
score=get<1>(tuplefunct(abc));
is there any alternative to this in c++ to store the values at once.
You dont need to call the function twice (note that there is no "another tuple" involved, the function returns one and thats what you use):
auto x = tuplefunct(abc);
auto n = get<0>(x);
auto score = get<1>(x);
If you have C++17 available you can use structured bindings
auto [n,score] = tuplefunct(abc);
Or to get close to that without C++17, you can use std::tie (from C++11 on):
node* n;
int score;
std::tie(n,score) = tuplefunct(abc);
I have an Apache arrow array that is created by reading a file.
std::shared_ptr<arrow::Array> array;
PARQUET_THROW_NOT_OK(reader->ReadColumn(0, &array));
Is there a way to convert it to std::vector or any other native array type in C++?
You can use std::static_pointer_cast to cast the arrow::Array to, for example, an arrow::DoubleArray if the array contains doubles, and then use the Value function to get the value at a particular index. For example:
auto arrow_double_array = std::static_pointer_cast<arrow::DoubleArray>(array);
std::vector<double> double_vector;
for (int64_t i = 0; i < array->length(); ++i)
{
double_vector.push_back(arrow_double_array->Value(i));
}
See the latter part of the ColumnarTableToVector function in this example:
https://arrow.apache.org/docs/cpp/examples/row_columnar_conversion.html. In that example, table->column(0)->chunk(0) is a std::shared_ptr<arrow::Array>.
To learn more, I found it useful to click on various parts of the inheritance diagram tree here: https://arrow.apache.org/docs/cpp/classarrow_1_1_flat_array.html. For example, strings in an arrow::StringArray are accessed using a GetString function instead of a Value function.
This is just what I've pieced together from these links, johnathan's comment above, and playing around with a small example myself, so I'm not sure if this is the best way, as I'm quite new to this.
I am doing some vector computations using the NE10 library. The library has a type for a complex number which is a struct like this:
typedef struct
{
ne10_float32_t r;
ne10_float32_t i;
} ne10_fft_cpx_float32_t;
I would like to have a function, which takes an array of these structs (1D array of complex numbers) as an argument and performs a vector computation for only the r or i elements of the array. Here is an example of such function to get an idea:
void multiple_real_part_by_two(ne10_fft_cpx_float32_t* output,
ne10_fft_cpx_float32_t* input, ne10_uint32_t array_length)
{
ne10_mulc_float_c (&output->r, &input->r, 2.0, array_length)
}
So I would like the output array to be like the input array, but each of the r elements should be multiplied by two. The problem is, the way the function is written above does not work, and leads to a segmentation fault. I think the problem is in how I am trying to pass an array of the r elements to the ne10_mulc_float_c() function.
The ne10_mulc_float_c() function takes as arguments pointers to two arrays of type ne10_float32_t of size array_length. The elements of the input array are multiplied by the number passed as the third argument, and the result is stored in the output array. The documentation can be found here.
Is there a way I could do this? I know I could just do this in a for loop
for (int i = 0; i < array_length; i++) {
output[i].r = input[i].r * 2.0
}
but I don't want to do this since performance is critical, which is why I am trying to use the vector operations provided by NE10 in the first place.
The problem is that you give ne10_mulc_float_c() arguments of the right type, but which do not match the assumptions.
According to the library's documentation page, the function is defined as:
ne10_result_t ne10_mulc_float_c (ne10_float32_t *dst, ne10_float32_t *src,
const ne10_float32_t cst, ne10_uint32_t count)
with the following arguments:
[out] dst Pointer to the destination array
[in] src Pointer to the source array
[in] cst The constant to multiply by
[in] count The number of scalar values to be processed
This means that the function assumes that dst and src are are pointers to arrays of count CONSECUTIVE floating point numbers.
Unfortunately, the is is not the case with the arguments that you pass:
&output->r and &input->r are both pointers to one single float. So as soon at this function tries to access the second item in the array it expects, it code goes beyond the array's real bounds and this is UB. This is why it doesn't work and you get the segmentation fault.
Your for-loop is just fine. Don't forget:
Premature optimization is the root of all evil.
-- Donald Knuth
I have a data structure in sparse compressed column format.
For my given algorithm, I need to iterate over all the values in a "column" of data and do a bunch of stuff. Currently, it is working nicely using a regular for loop. The boss wants me to re-code this as a for_each loop for future parallelization.
For those not familiar with sparse compressed column, it use 2 (or 3) vectors to represent the data. One vector is just a long list of values. The second vector is the index of where each column starts.
The current version
// for processing data in column 5
vector values;
vector colIndex;
vector rowIndex;
int column = 5;
for(int i = conIndex[5]; i != colIndex[6]; i++){
value = values[i];
row = rowIndex[i];
// do stuff
}
The key is that I need to know the location(as an integer) in my values column in order to lookup the row position (And a bunch of other stuff I'm not bothering to list here.)
If I use the std::for_each() function, I get the value at the position, not the position. I need the position itself.
One thought, and clearly not efficient, would be to create a vector of integers the same length as my data. That way, I could pass an iterator over that dummy vector to the function in for_each and the value passed to my function would be the postion. However, this seems like the least efficient way.
Any thoughts?
My challenge is that I need to know the position in the vector. for_each takes an iterator and sends the value of that iterator to the function.
Use boost::counting_iterator<int>, or implement your own.
#n.m.'s answer is probably the best, but it is possible with only what the standard library provides, though painfully slow I assume:
void your_loop_func(const T& val){
iterator it = values.find(val);
std::ptrdiff_t index = it - values.begin();
value = val;
row = rowIndices[index];
}
And after writing that, I really can only recommend the Boost counting_iterator version. ;)