Generating a point (vector/matrix) in OpenSCAD? - openscad

I'd like to create a polyhedron in OpenSCAD by generating its point vectors programmatically. However, assigning a value to a matrix...
p = [1,0,0];
r = [[], [], [], [], [], [] ];
for( i=[0:5] )
{
echo("i=",i);
r[i] = [0,1];
}
... produces a syntax error:
ERROR: Parser error in line 7: syntax error
ERROR: Compilation failed!
Am I making a mistake (which) or are these types really only read-only (no assignment by index)?

It's not possible to modify r after it was assigned.
See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions for expressions that can generate lists.
Example:
points = [ for (a = [0 : 5 : 359]) [ 20 * sin(a), 10 * cos(a) ] ];
polygon(points);

Related

Compress variable assignment into for loop in matlab [duplicate]

This question already has an answer here:
How to put these images together?
(1 answer)
Closed 4 years ago.
I want to compress part of my code into a for loop. The code looks like this:
dataMixSp1 = [dataSp1{1}; dataSp1{2}; dataSp1{3}; dataSp1{4};];
dataMixSp2 = [dataSp2{1}; dataSp2{2}; dataSp2{3}; dataSp2{4};];
dataMixSp3 = [dataSp3{1}; dataSp3{2}; dataSp3{3}; dataSp3{4};];
dataMixSp4 = [dataSp4{1}; dataSp4{2}; dataSp4{3}; dataSp4{4};];
dataMixSp5 = [dataSp5{1}; dataSp5{2}; dataSp5{3}; dataSp5{4};];
dataMixSp6 = [dataSp6{1}; dataSp6{2}; dataSp6{3}; dataSp6{4};];
dataMixSp7 = [dataSp7{1}; dataSp7{2}; dataSp7{3}; dataSp7{4};];
dataMixSp8 = [dataSp8{1}; dataSp8{2}; dataSp8{3}; dataSp8{4};];
dataMixSp9 = [dataSp9{1}; dataSp9{2}; dataSp9{3}; dataSp9{4};];
dataMixSp10 = [dataSp10{1}; dataSp10{2}; dataSp10{3}; dataSp10{4};];
The problem is that I don't know how to dynamically create variables inside the for loop.
I tried this but it is not working:
a = 'dataMixSp';
for idx = 1:10
[a num2str(idx)] = [['dataSp', num2str(idx), '{1}']; ['dataSp' num2str(idx) '{2}']; ['dataSp' num2str(idx) '{3}']; ['dataSp' num2str(idx) '{4}']; ];
end
To have Matlab evaluate an instruction in a string, use the eval function. For the problem you show, this could be done as:
root = 'dataMix';
part = 'dataSp';
for k=1:10
target = sprintf('%s%d', root,k); % creates strings 'dataMix1', 'dataMix2', ...
thisPart = sprintf('%s%d', part,k); %creates strings 'dataSp1, 'dataSp2', ...
rhs = '['; % the opening of the right-hand-side of the assignment
for n=1:4
rhs=sprintf('%s%s{%d};',rhs,thisPart,n);%appends 'dataSp1{1}, dataSp1{2}...
end
rhs=strcat(rhs(1:end-1),']');% close the right-hand-side
% note the (1:end-1) to remove the ';' between the last variable and the ']'
instruction = sprintf('%s = %s;',target,rhs); % create a Matlab instuction
eval(instruction) % have Matlab evaluate the instruction
end
Although, as #tryman said, sometimes the problem is easier to solve saving the data in a different way.
Hope this helps
JAC
for idx = 1:10
eval( [ 'dataMixSp' num2str(idx) '= [ dataSp' num2str(idx), '{1}; dataSp' num2str(idx) '{2}; dataSp' num2str(idx) '{3}; dataSp' num2str(idx) '{4} ];' ] )
end
Will execute the following code:
dataMixSp1= [ dataSp1{1}; dataSp1{2}; dataSp1{3}; dataSp1{4} ];
dataMixSp2= [ dataSp2{1}; dataSp2{2}; dataSp2{3}; dataSp2{4} ];
dataMixSp3= [ dataSp3{1}; dataSp3{2}; dataSp3{3}; dataSp3{4} ];
dataMixSp4= [ dataSp4{1}; dataSp4{2}; dataSp4{3}; dataSp4{4} ];
dataMixSp5= [ dataSp5{1}; dataSp5{2}; dataSp5{3}; dataSp5{4} ];
dataMixSp6= [ dataSp6{1}; dataSp6{2}; dataSp6{3}; dataSp6{4} ];
dataMixSp7= [ dataSp7{1}; dataSp7{2}; dataSp7{3}; dataSp7{4} ];
dataMixSp8= [ dataSp8{1}; dataSp8{2}; dataSp8{3}; dataSp8{4} ];
dataMixSp9= [ dataSp9{1}; dataSp9{2}; dataSp9{3}; dataSp9{4} ];
dataMixSp10= [ dataSp10{1}; dataSp10{2}; dataSp10{3}; dataSp10{4} ];
Arno

Callback in Bender's decomposition

I am learning Bender's decomposition method and I want to use that in a small instance. I started from "bendersatsp.py" example in CPLEX. When I run this example with my problem, I got the following error. Could you please let me know what the problem is and how I can fix it? In the following you can see the modifications in lazy constraints function. I have two decision variables in master problem "z_{ik}" and "u_{k}" that would incorporate as constant in the workerLp.
class BendersLazyConsCallback(LazyConstraintCallback):
def __call__(self):
print("shoma")
v = self.v
u = self.u
z = self.z
print ("u:", u)
print ("z:", z)
workerLP = self.workerLP
boxty = len(u)
#scenarios=self.scenarios2
ite=len(z)
print ("ite:", ite)
print ("boxty:", boxty)
# Get the current x solution
sol1 = []
sol2 = []
sol3 = []
print("okkkk")
for k in range(1, boxty+1):
sol1.append([])
sol1[k-1]= [self.get_values(u[k-1])];
print ("sol1:", sol1[k-1])
for i in range(1, ite+1):
sol2.append([])
sol2[i-1]= self.get_values(z[i-1]);
print ("sol2:", sol2[i-1])
for i in range(1, ite+1):
sol3.append([])
sol3[i-1]= self.get_values(v[i-1]);
#print ("sol3:", sol3[i-1])
# Benders' cut separation
if workerLP.separate(sol3,sol1,sol2,v,u,z):
self.add(cut = workerLP.cutLhs, sense = "G", rhs = workerLP.cutRhs)
CPLEX Error 1006: Error during callback.
benders(sys.argv[1][0], datafile)
cpx.solve()
_proc.mipopt(self._env._e, self._lp)
check_status(env, status)
raise callback_exception
TypeError: unsupported operand type(s) for +: 'int' and 'list'

Complex Number from square root of negative array [duplicate]

This question already has answers here:
How can I take the square root of -1 using python?
(6 answers)
Closed 5 years ago.
I am a new convert from Matlab to python and I am struggling with the generation of a complex array.
In matlab I have the following code:
xyAxis = linspace(-127,127,255);
[x,y] = meshgrid(xyAxis, xyAxis);
fz = -(x.^2 + y.^2);
ifz = sqrt(fz);
Which I am trying to replicate in python 3:
import numpy as np
xyAxis = np.intp( np.linspace(-127, 127, 255) )
x, y = np.meshgrid(xyAxis,xyAxis)
fz = - (x**2 + y**2)
ifz = np.sqrt(fz)
However, I get the following error:
RuntimeWarning: invalid value encountered in sqrt
I have done a bit of googling and I am not sure how to mimic the behavior of matlab in this case? Any suggestions?
One way is casting fz to complex dtype:
ifz = np.sqrt(fz.astype(np.complex))
Another way apart from what #PaulPanzer suggested -
import numpy as np
xyAxis = np.intp( np.linspace(-127, 127, 255) )
x, y = np.meshgrid(xyAxis,xyAxis)
fz = - (x**2 + y**2)
from numpy.lib.scimath import sqrt as csqrt
ifz = csqrt(fz)
print ifz
This comes straight from the numpy docs
Output
[[ 0.+179.60512242j 0.+178.89941308j 0.+178.19652073j ...,
0.+178.19652073j 0.+178.89941308j 0.+179.60512242j]
[ 0.+178.89941308j 0.+178.19090886j 0.+177.48521065j ...,
0.+177.48521065j 0.+178.19090886j 0.+178.89941308j]
[ 0.+178.19652073j 0.+177.48521065j 0.+176.7766953j ...,
0.+176.7766953j 0.+177.48521065j 0.+178.19652073j]
...,
[ 0.+178.19652073j 0.+177.48521065j 0.+176.7766953j ...,
0.+176.7766953j 0.+177.48521065j 0.+178.19652073j]
[ 0.+178.89941308j 0.+178.19090886j 0.+177.48521065j ...,
0.+177.48521065j 0.+178.19090886j 0.+178.89941308j]
[ 0.+179.60512242j 0.+178.89941308j 0.+178.19652073j ...,
0.+178.19652073j 0.+178.89941308j 0.+179.60512242j]]

Error in Matlab, calling a function

The errors are:
Error in getvalues (line 4)
faceNoNoise = wiener2(x, [5 5]);
Output argument "mouthTall" (and maybe others) not assigned during call to "C:\Users\Trent\face\getvalues.m>getvalues".
Error in finalProject2 (line 10)
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray )
faceNoNoise = wiener2(x, [5 5]); <---- getvalues.m line with error
function finalProject2(x) <----- finalproject file
face = imread(x);
faceGray = rgb2gray(face);
numWhiteEyebrow = 0;
mouthTall = 0;
eyebrowHeight = 0;
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray ) <--error above
end
Im trying to figure out why its doing this...
This happens since you have not assigned required output values in the function getvalues.m. There are 3 output arguments which should be returned by getvalues.m. See this and this. To clarify even more, if your function definition in getvalues.m file is [a,b,c]=getvalues(x) (and this is how it would mostly be in your case) then in your getvalue.m file, you should have variables a, b and c.

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1