I've written what I thought would be a working MUX, but my output is stubbornly staying at high-impedance. Can someone please provide me with guidance?
module mux_in #(parameter WIDTH = 1, parameter LOG_CHOICES = 1)
(
input [LOG_CHOICES - 1 : 0] choice,
input [(1 << LOG_CHOICES) * WIDTH - 1 : 0] data_i,
output [WIDTH - 1 : 0] data_o
);
assign data_o = data_i[WIDTH * choice + WIDTH - 1 : WIDTH * choice];
endmodule
Here's my (bad) output:
data_i: 11111010101111100001001100100010
data_o: zzzzzzzz
Choice 0: (Expected 34) Output: z
Choice 1: (Expected 19) Output: z
Choice 2: (Expected 190) Output: z
Choice 3: (Expected 250) Output: z
This should not compile because the range expression is not constant.
assign data_o = data_i[WIDTH * choice + WIDTH - 1 : WIDTH * choice];
Try this instead.
assign data_o = data_i[WIDTH * choice + WIDTH - 1 -: WIDTH];
Related
First of all, I`m sorry if I might ask this question and if it was already answered somewhere else. I couldnt find any solution for the following Problem:
I want to create a list where I apply multiple restricions. But instead of using over hundreads of if statements i just want to use a dictionary in one if statement to apply the requirements. So to say I want to use the keys of the dictionary as requirements and its values as factors for the data.
Lets take a look at a small example:
I want to create data for a countourplot where x/y range from [-50,50] in steps of 1 and the z function has requirements based on the if statements:
The following code is what works, but the more requirements I add the longer and more unreadalbe the code gets:
x = np.linspace(-50 , 50, 100)
y = np.linspace(-50 , 50, 100)
z = []
z_0 = 100
for i in x:
for j in y:
if i**2 + j**2 <= 10**2:
z.append(1.9 * z_0)
elif i**2 + j**2 <= 20**2:
z.append(1.5 * z_0)
elif i**2 + j**2 <= 30**2:
z.append(1.4 * z_0)
elif i**2 + j**2 <= 40**2:
z.append(1.05 * z_0)
else
z.append(z_0)
This would create a map with radial decreasing hight as a function of z on different positions. Is it possible to do this in the following way which is way less redundant? My main problem is how to assing the correct value.
x = np.linspace(-50 , 50, 100)
y = np.linspace(-50 , 50, 100)
z = []
requirements_dict = {10:1,9, 20:1.5, 30:1.4, 40:1.05}
z_0 = 100
for i in x:
for j in y:
if i**2 + j**2 <= (each key of the requirements_dict) :
z.append( (corresponding value of the requirements dict) * z_0)
else
z.append(z_0)
Thanks in advance for the help and sorry again if this question was already asked.
Is this what you're looking for?
EDIT:
import numpy as np
x = np.linspace(-50 , 50, 100)
y = np.linspace(-50 , 50, 100)
z = []
requirements_dict = {10: 1.9, 20: 1.5, 30: 1.4, 40: 1.05}
z_0 = 100
for i in x:
for j in y:
for key, value in requirements_dict.items():
if i**2 + j**2 <= key:
z.append(value * z_0)
break
else:
z.append(z_0)
I have set returns on few stocks, for example;
A
B
C
D
.10
.12
.09
.11
I know that I am aiming to allocate the weights in each stock so that I achieve a payoff of .115. How do I solve for this? Im don't think using matrix would work as I the return is one dimensional?
Let's denote a, b, c, d as the weight of each stock in the portfolio. We are trying to maximize the portfolio's return:
maximize r = 0.1a + 0.12b + 0.09c + 0.11d
Subject to the following constraints:
0 < a, b, c, d <= 1
a + b + c + d = 1
0.1a + 0.12b + 0.09c + 0.11d = 0.115
We will use scipy.optimize.linprog to solve this. The function has a peculiar way of accepting inputs:
It always seeks to minimize the objective function
It expects constraints in 2 forms: A_ub * x <= b_ub and A_eq * x = B_eq
So we need to restate our problem to make it fit with the way linprog works:
minimize r' = -0.1a - 0.12b - 0.09c - 0.11d
Subject to:
0.0001 <= a,b,c,d <= 1 # a near-zero lowerbound since
# we can't use half-open range
1a + 1b + 1c + 1d = 1
0.1a + 0.12b + 0.09c + 0.11d = 0.115
Code:
from scipy.optimize import linprog
c = [-0.1, -0.12, -0.09, -0.11]
A = [
[1, 1, 1, 1],
[0.1, 0.12, 0.09, 0.11],
]
b = [
1,
0.115,
]
result = linprog(c, A_eq=A, b_eq=b, bounds=(0.0001, 1))
Output:
>>> result
con: array([-9.16875464e-11, -9.32195987e-12])
fun: -0.11500000000932195
message: 'Optimization terminated successfully.'
nit: 4
slack: array([], dtype=float64)
status: 0
success: True
x: array([0.1159907 , 0.64131022, 0.01265976, 0.23003933])
Of course there many solutions to this problem. You can get the optimal weights with result.x.
b = [3,2,6]
hid = []
#print(b[0] * b[1])
#print(b[1] * b[2])
#print(b[2] * b[3])
for n in range(len(b)):
print(b[n-1] * b[n])
The result I am expecting is
6, 12
But I am getting
18, 6, 12
Where is the mistake?
range(len(b)) which evaluate as (0,1,2)
so b[n-1] is b[-1] which is 6 so (6 * 3) =18 it give result
18, 6, 12
try This one:
for n in range(len(b)-1):
print(b[n] * b[n+1])
Your list has three elements only, so there's no b[3], what you're trying to achieve (multiplying combinations of size 2) can be done using two nested loops:
b = [3, 2, 6]
for i in range(len(b) - 1):
for j in range(i + 1, len(b)):
print(b[i] * b[j])
Output:
6
18
12
However, a more Pythonic way would be to use itertools.combinations:
b = [3, 2, 6]
from itertools import combinations
for x, y in combinations(b, 2):
print(x * y)
Output:
6
18
12
for n in range(len(b)):
print(b[n-1] * b[n])
in first iteration when n =0 you have:
print(b[n-1] * b[n])
what means:
print(b[0-1] * b[0])
b[0-1] will gave you last element in the list, because of that you have
6*3 = 18
you should do it:
b = [3,2,6]
hid = []
for n in range(1,len(b)):
print(b[n-1] * b[n])
range(3) will generate numbers from 0 to 2 (3 numbers). You can also
define the start, stop and step size as range(start,stop,step size).
step size defaults to 1 if not provided.
or
b = [3,2,6]
hid = []
for n in range(len(b)-1):
print(b[n] * b[n+1])
output:
6
12
In a for loop, the iteration starts at zero
for n in range(len(b)):
print(b[n-1] * b[n]) doing "n-1" means b[-1] which is last element of your list "6" and this with b[n] which b[0] is 18.
So, your code translates to :
print b[-1]*b[0]
print b[0]*b[1]
print b[1]*b[2]
Correct way to code this would be :
#!/usr/bin/python
b = [3,2,6]
i = 0
while i < len(b)-1:
print b[i]*b[i+1]
i+=1
Output:
6
12
I wrote a code in python 3 but I get an error on the following line of code:
if x > blacks[i*2] and y < blacks[(i*2)+1] and ((x - blacks[i*2]) / (blacks[(i*2)+1]-y) <= 1 :
I don't think the problem is related with the rest of the code because I get the error even when I try using this 'if statement' in a very simple code:
blacks = [0,0,0,0,0]
i = 1
x = 0
y = 0
if x > blacks[i*2] and y < blacks[(i*2)+1] and ((x - blacks[i*2]) / (blacks[(i*2)+1]-y) <= 1 :
blacks[i * 2] = blacks[i * 2]+4
blacks[(i * 2) + 1] = blacks[(i * 2)+1] - 2
Am I missing something really obvious?
The problem is right here
(x - blacks[i*2]) / (blacks[(i*2)+1]-y)
Both of those values on either side of the / evaluate to 0, therefore you're dividing 0 by 0.
You're missing the right parentheses at the end of the expression in the if statement, or rather you have an extra left parentheses before (x - blacks[i*2]), which should be corrected as follows:
blacks = [0,0,0,0,0]
i = 1
x = 0
y = 0
if x > blacks[i*2] and y < blacks[(i*2)+1] and (x - blacks[i*2]) / (blacks[(i*2)+1]-y) <= 1:
blacks[i * 2] = blacks[i * 2]+4
blacks[(i * 2) + 1] = blacks[(i * 2)+1] - 2
I am making a dithering library. To find the relative position of an absolute point a in a 2-dimensional plane tiled with 4 unit squares, I use rel.x = abs.x % 4; rel.y = abs.y % 4. This is good, and produces the expected results. But what if I am tiling the plane with plus shapes, which are 3 units? How do I find the absolute position? The tile shape is showed here, 1's are parts of the shape, and 0's are empty areas.
0 1 0
1 1 1
0 1 0
For example, if I have point a resting on x = 1, y = 1, then the absolute position should be x = 1, y = 1. But if it is on, say x = 4, y = 1, then the absolute position should be x = 1, y = 2. You see, there would be another plus which's bottom is on the point x = 1, y = 2. How is this accomplished mathematically? Any language, pseudo code is great too. :)
There is periodicity along X and Y axes with period 5. So long switch expression might look like:
case y % 5 of:
0: case x % 5 of
0: cx = x - 1; cy = y;
1: cx = x; cy = y + 1;
2: cx = x; cy = y - 1;
3: cx = x + 1; cy = y;
4: cx = x; cy = y;
1:...
Or we can create constant array 5x5 and fill it with shifts -1, 0, 1.
dx: [[-1,0,0,1,0],[1,0,-1,0,0],[0,0,1,0,-1],[0,-1,0,0,1],[0,1,0,-1,0]]
dy: [[0,1,-1,0,0],[0,0,0,1,-1],[1,-1,0,0,0],[0,0,1,-1,0],[-1,0,0,0,1]]
I feel that some simple formula might exist.
Edit: simpler version:
const dx0: [-1,0,0,1,0]
const dy0: [0,1,-1,0,0]
ixy = (x - 2 * y + 10) % 5;
dx = dx0[ixy];
dy = dy0[ixy];
And finally crazy one-liners without constant arrays
dx = (((11 + x - 2 * (y%5)) % 5) ^ 1 - 2) / 2 //^=xor; /2 - integer division
dy = ((13 + x - 2 * (y%5)) % 5 - 2) / 2