pushing a big float inside a tuple Julia - tuples

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)

Related

Storing 2 variables at once from a tuple function

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);

Efficient way to declare and access a huge array

I am writing a code for which I need to declare an array of around 200 indexes. Now to search a specific index I at least need to travel the array for a defined time or until desired value is achieved - hence at times I might need to travel 200 times if needed - for 200 value row.
This is exactly I wish to ignore so I landed coding it below way:
enum Index{ salary, age };
static const Datas Mydata [] =
{
[Index::one] = {"hello", function_call_ptr_1, function_call_ptr_2},
[Index::two] = "hekllo1", function_call_ptr_1, function_call_ptr_2}
};
Hence in my code I can directly seek it like below:
Mydata [Index::age]
Assuming that access to above structure is to be done inside a function - hence the function should receive Index value as argument to itself. But still what if arguments passed was wrong like:
age = 0;
fun(age);
Is there a better way to access Mydata so that its desired row can easily be accessed without any flaw?

Octave value list datatype

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

MPI_ALLGATHERV with derived data types

I am trying to use MPI_ALLGATHERV using derived data types. Actually, I have to pass chunks of small 3D array in the form of:
SS(IIST:IIEND,JJST:JJEND,KKST:KKEND)
Here, IIST, IIEND, JJST, JJEND, KKST, KKEND are local indices of each process. So I tried to define a derived datatype in the following form:
INTEGER :: MPII,MPJJ,MPKK
CALL MPI_TYPE_CONTIGUOUS(IIEND-IIST+1,MPI_DOUBLE_PRECISION,MPII,IERR)
CALL MPI_TYPE_CONTIGUOUS(JJEND-JJST+1,MPII,MPJJ,IERR)
CALL MPI_TYPE_CONTIGUOUS(KKEND-KKST+1,MPJJ,MPKK,IERR)
CALL MPI_TYPE_COMMIT(MPKK,IERR)
Now, I am defining a displacement array which is visible to every process to be used in MPI_ALLGATHERV. The total number of processes is 27 and they are numbered from 0-26.
DO NT=0,26
DISPL(1)=0
IF (NT.GT.0) DISPL(NT+1)= DISPL(NT)+1
ENDDO
Now, I am executing MPI_ALLGATHERV with the following syntax:
CALL MPI_ALLGATHERV(SS(IIST:IIEND,JJST:JJEND,KKST:KKEND),SPANX*SPANY*SPANZ,MPI_DOUBLE_PRECISION,SS(1,1,1),1,DISPL,MPKK,MPI_COMM_WORLD,IERR)
This is giving me error. Any pointers in this problem will be very helpful and appreciated.
spanx = iiend-iist+1
spany = jjend-jjst+1
spanz = kkend-kkst+1
oldsize = (/spanx,spany,spanz/)
newsize = (/spanx,spany,spanz/)
starts = (/0,0,0/)
CALL MPI_TYPE_CREATE_SUBARRAY(3,OLDSIZE,NEWSIZE,STARTS,MPI_ORDER_FORTRAN,MPI_DOUBLE_PRECISION,ARR,IERR)
CALL MPI_TYPE_COMMIT(ARR,IERR)
DO NT=0,26
DISPL(1)=0
IF (NT.GT.0) DISPL(NT+1)= DISPL(NT)+1
SIZECC(NT)=1
ENDDO
CALL MPI_ALLGATHERV(SS(IIST:IIEND,JJST:JJEND,KKST:KKEND),SPANX*SPANY*SPANZ,MPI_DOUBLE_PRECISION,SS(1,1,1),SIZECC,DISPL,ARR,MPI_COMM_WORLD,IERR)
Still the output doesn`t match. I think something is wrong in Displacement array.

PyTuple_Pack which takes in variable number of arguments

I am on Python 2.6.6 & calling python from c++ .
How can i write PyTuple_Pack which takes in variable number of arguments .
I have variable arguments coming which I use a for loop and populate my array . My array has max of 1000 elements but i populate only top no_of_argument elements using
python_obj[i] = PyString_FromString(arg[i])
I DO NOT want to write code like
if(no_of arg ==1 )
return PyTuple_Pack(1, python_obj[0]);
if(no_of arg ==2 )
return PyTuple_Pack(2, python_obj[0],python_obj[1]);
I want something like
return PyTuple_Pack(no_of_args, followed by my array where pyobjects exist ;
On net i can see http://docs.python.org/release/2.4.4/whatsnew/node14.html
A new function, PyTuple_Pack(N, obj1, obj2, ..., objN), constructs tuples from a variable length argument list of Python objects. (Contributed by Raymond Hettinger.)
However example to write good code using above is not found by my search for which i need your help pls..
Use PyTuple_New to make a tuple with the right size, then iterate over your array and use PyTuple_SetItem to fill the tuple with values.