PyTuple_Pack which takes in variable number of arguments - c++

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.

Related

pushing a big float inside a tuple Julia

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)

(wx)Maxima: apply scalar function to all elements of a list

Currently, if I want to apply the transformation effected by a function of a single variables f to all values in a list, I use makelist, e.g.
f(x):= x^2;
aList: [1,2,3,4];
f_aList: makelist(f(aList[i]), i, length(aList));
I have to do this so often that I was about to write a quick function that would handle this, e.g.
apply2list(f, aList):=
block([f:f, aList:aList],
makelist(f(aList[i]), i, length(aList))
);
for any single variable function f and list aList, but wondered if there was some way of doing this, already built in?

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

Heterogeneous container of base class when the derived instances are not pointers

I have a base class and I want to store instances of its derivatives in a collection of some sort.
At first I created a map:
std::map<int, Variable> varriableItems;
and then ussing templates I created functions for each derivative and I tried passing in the derivatives like so:
template <>
void Array::addToMap<Number>(Number input)
{
numberVariables[itemCount_] = input;
itemCount_++;
}
By doing so this function was not called because everything was of type Variable of course and I found out about slicing.
So instead I changed my map to take in pointers to my base class
std::map<int, Variable*> varriableItems;
but the problem I have is that all my objects are not created as pointers so I could not pass them in and I was getting errors.
No suitable conversion from "Number" to "Variable" exists.
Due to my implementation I can only create instances of objects
like so:
auto aNumberVariable = Number{50};
Ofcourse if I instead do:
Number aNumberVariable = new Number(50);
it works great.
The reason am doing this is explained bellow.
Please bear with me because this is a weird assignment.
We were asked to create a program that behaves/understands the syntax of a programming language called Logo, without actually analyzing the text as an input file, but rather "disguise" it to appear as such while in fact we just use C++ using what we learned from C++ and lots of overloads and pre-processor tricks
We have to be able to make our own "types" of variables called NUMBER,WORD,BOOLEAN,ARRAY, LIST,SENTENCE.
To declare them we have to use(note no semi-colons should be used):
//define number variable with value 21
MAKE number = NUMBER: 21
//define hello variable with value “hello”
MAKE hello = WORD: “hello”
//define myMoves variable contains list of turtle moves
MAKE myMoves = LIST [
LIST [WORD: “FORWARD”, NUMBER: 100],
LIST [WORD: “LEFT”, NUMBER: 90],
LIST [WORD: “FORWARD”, NUMBER: 100]
]
//define array variable with empty array
MAKE array = ARRAY {
number,
hello,
NUMBER: 12
BOOLEAN: TRUE,
ARRAY {
myMoves,
LIST [WORD: “BACK”, NUMBER: 100]
}
}
//define book variable with sentence type
MAKE book = SENTENCE (hello, WORD: “hello!”)
That's just a small part, we later have to support functions, nested loops , etc.
So do this I have to find a way to use the colon since I cannot overload it, so I did this:
//Create an instance of Number and write the first half of the ternary operator so we
//always get the false value so we can use the : like this
#define NUMBER Number{} = (false) ? 0
//semicolon infront for the previous command that needs it
#define MAKE ;auto
So now this:
//following commands will deal with the semicolon
MAKE myNumber = NUMBER: 21
worked great and it actually gets replaced by the processor to this:
auto myNumber = Number{} = (false) ? 0 : 21
So i worked with this for all my derivatives and I proceeded to overload operators to compare them, implement if else function in a similarly weird syntax.
Now I either have to figure out a way to make this work again but this time creating them as pointer instead (Which I assume is the only way for this to work, but I so far I couldn't figure it out) or create a single class for all types but doing it in separate objects that all inherit from a single base class makes more sense to me.
And am not sure how strict they will be, it is an unconventional project assignment for sure.
The reason I want to hold them together in a container is so I can then implement an Array and list object that can hold every type. At first I tried to use a different container for each type and made an iterator to iterate multiple maps separately, but when I got to the LIST implementation things got weird.
The list syntax is using the brackets [ ] which can only get 1 input value, so the idea was to collect them by overloading the comma operator and pass in one value to the list object.
I know this is weird , thank you for your time
I didn't read through all of your post. (actually I did because your task is so ... beyond words) but if you need polymorphism in a container and you also need the container to hold the objects, then the solution is unique_ptr:
container<std::unique_ptr<Base>>
In your case it would go something along this:
std::unordered_map<int, std::unique_ptr<Variable>> varriableItems;
varriableItems[0] = std::make_unique<Number>(50);

return an array instead of a tuple from a NumPy function

I have the following code:
return atleast_1d(0.4*P2), atleast_1d(k_uni)
My function returns a tuple. What is the quickest way to make this return as an array?
This return statement creates a tuple, consisting of 2 arrays.
return atleast_1d(0.4*P2), atleast_1d(k_uni)
You'd get a tuple if you did
return 1, 2
return [1,2,3],{'one':1}
etc
I'd suggest calling the function with
x, y = foo(...)
Now the 2 arrays defined in the return statement are assigned to 2 variables. You can combine them in what ever way that works (depends on their shape).
np.array((x,y))
np.concatenate((x,y))
etc
Note that I am actually giving those functions a tuple. I could have written it as a list.
The issue isn't how the function returns a tuple, but how you want to combine 2 arrays into one.