Error_Generator expression_spyder(python 3.6) - expression

I develop mathematical model using gurobi solver, in python.
I get the following error while running:
SyntaxError: Generator expression must be parenthesized if not sole argument
My constraint is:
My code is:
for s in S:
m.addConstr(sum(x[s,s0,c,i] for s0 in S0 for c in C for i in D,s!=p) == 1,'C_3')

First of all, everything comes whenever you add that comma: ,s!=p.
I just emulated your code with a model I'm working on, and I obviously got the same error. Look around (e.g. if else in a list comprehension), and you will see that the only mistake you had was that the iterator within the generator wasn't well specified. That means, you had to use an if clause in order to achieve what you wanted:
for s in S:
m.addConstr(
quicksum(x[s,s0,c,i] for s0 in S0 for c in C for i in D if s!=p) == 1,
'C_3_'+str(s) )
By the way, as included in the code, you should use quicksum instead of sum. Furthermore, I would suggest to try changing the order of the iterators; in other words, it's not the same for a computer to enumerate a list of 5 elements 1000 times than to enumerate 5 times a list of 1000 elements, and this is something quite important in Python timing.
As a side note, I got into this question while looking for this:
TypeError: unsupported operand type(s) for +: 'generator' and 'generator'

Related

Error when trying to sum two products, where one product is a 1x2 matrix

First question that I've posted here, I'm working on some code for my final on Monday and for some reason I keep getting the following error. Any help would be MASSIVELY appreciated. Thanks!;
The following imagine shows the error in my code, I had switched to 1D math and still can't seem to find the issue
In Maple the global name D has a preassigned value. It is the differential operator. For example,
f := t -> sec(t):
D(f);
t -> sec(t)*tan(t)
The global name D is also protected, ie. you cannot assign another values to it.
And in general it is not a good idea to use the name D like a dummy variable in your code (since is has already been assigned a system procedure, by default). Your example is an (unfortunate) example of strangeness that could ensue.
D * Vector([-3,1]);
Error, (in LinearAlgebra:-Multiply) invalid arguments
You have a couple of alternatives:
1) Use another name instead, such as DD.
2) If you Maple version is recent then you could declare it (once) as a local name for top-level use. For example,
restart;
local D;
D
D * Vector([-3,1]);
[-3 D]
[ ]
[ D ]
If you do declare local D for top-level use then you can still utilize the differential operator by its global name :-D. Eg.
restart;
local D:
f := t -> sec(t):
D(f); # does nothing, since this is the local D
D(f)
:-D(f);
t -> sec(t)*tan(t)
If all that sounds confusing, you'll probably be better off just using another name instead.
The are only a few short symbols with preassigned values or uses, eg. Pi, I, D.
You might want to also have a look at the Help page for Topics initialconstants and trydeclaringlocal .

How to ignore one or more output bus pins for a module instantiation

I want to ignore one or more bits in an array argument for a module in SystemVerilog.
module x(in a, out [0:3] z);
...
endmodule
module tb;
logic my_a;
logic [1:3] my_z;
// I want to stop having to do this next line:
logic ignore_this_bit;
x myx(.a(my_a), .z({ignore_this_bit, my_z}));
endmodule
What is the proper syntax to do this? I have been doing it just as shown above with a declaration of ignore_this_bit and simply never connecting to that net. But it seems there should be a simpler way. Something like just using a comma and no variable name in the arguments for the module instantiation or maybe using something like 1'bX instead of an output argument bit, or something like that.
Is this affected by the fact that I am using big-endian bit ordering for the vectors here? (I hate it, but I am building code for an old CPU that uses that ordering and it's way easier to match my code to the existing than to fix it.)
This is a hard concept to search for, and I have tried. Does anyone have expertise that can help me know how to do this "the right way"? Thanks.
There is no special way in verilog for ignoring bits of the output. So, your way of using concat with an unneeded variable is a good way for doing it ({ignore_this_bit, my_z}). Naming of this variable is important for readability reasons.
It is not affected by the range description order. It looks like you are ignoring the left-most bit. And the bits are always ordered in the same way, no matter how you describe the range:
bits: 0011
[3:0]: 3 0
[0:3]: 0 3
concat: {ign, 0, 1, 1};
The other way around is to use a variable big enough to connect to the output and then use its bits:
logic [1:3] my_z;
logic [0:3] their_z;
x myx(.a(my_a), .z(their_z));
assign my_z = their_z[1:3];
You should not need to do anything here. This should just work truncating the MSB z[0]
x myx(.a(my_a), .z(my_z));
Think of an output port as an implicit continuous assignment
assign my_z = myx.z;
But if the MSB is not the bit you want to ignore, there is no simple solution. You might want to look at the net alias feature.

A views implementation for cg! on matrices

I am trying to use the cg! function from IterativeSolvers.jl on for solving matrix-matrix linear systems, i.e. AX=B for A,X,B appropriately sized matrices. Given the way the indices work, X[:,i] is independent of all but B[:,i], and so this actually boils down to n different linear solves. Direct solving via \ works automatically in this case, but iterative solvers like CG don't. I can easily do this with a loop on the outside, but I haven't been able to get the in-place op working. For now, my code looks like this:
for j=1:size(u,2)
u[freenode,j],ch = cg!(u[freenode,j],lhs,Dinv.*rhs(u,i)[:,j]) # Requires Vector, need to change rhs
end
which gives solves CG with the appropriate lefthand side and righthand side. But the reason why it's not in-place boils down to this simple example throwing an error:
using IterativeSolvers
y = view(ones(4,2),:,2)
A=rand(4,4)
cg!(y,A,view(zeros(4,2),:,2))
which is:
ERROR: MethodError: no method matching init!
(::IterativeSolvers.KrylovSubspace{Float64,Array{Float64,2}}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true})
Closest candidates are:
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1}) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\krylov.jl:66
in #cg!#23 at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:7 [inlined]
in cg!(::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Array{Float64,2}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Int64) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:6 (repeats 2 times)
The problem seems to not be with the views, given the results of a previous SE question
I have doubts that you'll be able to avoid allocations, because the init! function is implemented as
function init!{T}(K::KrylovSubspace{T}, v::Vector{T})
# K.v = Vector{T}[all(v.==zero(T)) ? v : v/norm(v)]
K.v = Vector{T}[copy(v)]
end
and hence there's a copy anyway. Nevertheless, if you want this function to accept views, it should not be an issue to just modify the Vector to AbstractVector. (The function is simple enough that if you don't like to modify the package, you can just add a more general method yourself.)
You're right, it's not a "view" problem per se, the problem seems to be that the init! method seems to not have been defined in a way that can accept a 'view' (i.e. a SubArray type). The error suggests that "the closest candidate is"
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1})
(i.e. there's no method definition for a SubArray, only for a normal Array, and it appears there's no generic function / base case available to fall back on)
If you 'collect' the array first, then it works (presumably) as expected:
julia> cg!( collect(y), A, view(zeros(4,2),:,2))
([0.0752658,-0.693794,0.330172,0.437474],IterativeSolvers.ConvergenceHistory{Float64,Array{Float64,1}}(false,0.0,5,[0.249856,0.392572,0.401496,0.463142]))
except obviously that's of no use to you because you don't really get to change y as intended this way.
The only way I see around it if you're intent on keeping y as a view until that point, is to temporarily 'hack' it into a normal array inside cg! using a compound statement:
cg!((y = collect(y); y),A,view(zeros(4,2),:,2))
but, obviously, it's no longer a view at this point, so you'd have to update the original array it was a view into manually ...

Halide::Expr' is not contextually convertible to 'bool' -- Storing values of functions in variables

I am new to using Halide and I am playing around with implementing algorithms first. I am trying to write a function which, depending on the value of the 8 pixels around it, either skips to the next pixel or does some processing and then moves on to the next pixel. When trying to write this I get the following compiler error:
84:5: error: value of type 'Halide::Expr' is not contextually convertible to 'bool'
if(input(x,y) > 0)
I have done all the tutorials and have seen that the select function is an option, but is there a way to either compare the values of a function or store them somewhere?
I also may be thinking about this problem wrong or might not be implementing it with the right "Halide mindset", so any suggestions would be great. Thank you in advance for everything!
The underlying issue here is that, although they are syntactically interleaved, and Halide code is constructed by running C++ code, Halide code is not C++ code and vice versa. Halide code is entirely defined by the Halide::* data structures you build up inside Funcs. if is a C control flow construct; you can use it to conditionally build different Halide programs, but you can't use it inside the logic of the Halide program (inside an Expr/Func). select is to Halide (an Expr which conditionally evaluates to one of two values) as if/else is to C (a statement which conditionally executes one of two sub-statements).
Rest assured, you're hardly alone in having this confusion early on. I want to write a tutorial specifically addressing how to think about staged programming inside Halide.
Until then, the short, "how do I do what I want" answer is as you suspected and as Khouri pointed out: use a select.
Since you've provided no code other than the one line, I'm assuming input is a Func and both x and y are Vars. If so, the result of input(x,y) is an Expr that you cannot evaluate with an if, as the error message indicates.
For the scenario that you describe, you might have something like this:
Var x, y;
Func input; input(x,y) = ...;
Func output; output(x,y) = select
// examine surrounding values
( input(x-1,y-1) > 0
&& input(x+0,y-1) > 0
&& ...
&& input(x+1,y+1) > 0
// true case
, ( input(x-1,y-1)
+ input(x+0,y-1)
+ ...
+ input(x+1,y+1)
) / 8
// false case
, input(x,y)
);
Working in Halide definitely requires a different mindset. You have to think in a more mathematical form. That is, a statement of a(x,y) = b(x,y) will be enforced for all cases of x and y.
Algorithm and scheduling should be separate, although the algorithm may need to be tweaked to allow for better scheduling.

Why does whitespace in my F# code cause errors?

I've been tinkering with the F# Interactive.
I keep getting weird results, but here's one I can't explain:
The following code returns 66, which is the value I expect.
> let f x = 2*x*x-5*x+3;;
> f 7;;
The following code throws a syntax error:
> let f x = 2*x*x - 5*x +3;;
stdin(33,21): error FS0003: This value is not a function and cannot be applied
As you can see, the only difference is that there are some spaces between the symbols in the second example.
Why does the first code example work while the second one results in a syntax error?
The problem here is the use of +3. When dealing with a +/- prefix on a number expression white space is significant
x+3: x plus 3
x +3: syntax error: x followed by the positive value 3
I've run into this several times myself (most often with -). It's a bit frustrating at first but eventually you learn to spot it.
It's not a feature without meaning though. It's necessary to allow application of negative values to functions
myFunc x -3: call function myFunc with parameters x and -3
The error message says that you are trying to call a function x with the argument +3 ( unary + on 3) and since x is not a function, hence the This value is not a function and cannot be applied

Resources