How can I get result with array in Sympy - arrays

I am tring to get countour chart with sympy
I'm tring something like below but subs does not take array
and I tried for lambapy but lamdafy does not take 2 symbols or I don't know how to.
X,Y, formula = symbols('X Y formula')
formula = sp.sympify('X*2 + Y*3 +7*X*Y +34')
x = numpy.arange(1,10,1)
y = numpy.arange(1,10,1)
XValue,YValue = meshgrid(x,y)
ZValue = formula.sub([(X,XValue),(Y,YValue)])
Plot.contour(XValue, YValue, ZValue)
Are there any way to get result form 2 or more symbol with arrays

Answer was to lambdify the formula and get the result Z first. Then put XYZ value into the chart
X,Y, formula = symbols('X Y formula')
formula = sp.sympify('X*2 + Y*3 +7*X*Y +34')
x = numpy.arange(1,10,1)
y = numpy.arange(1,10,1)
XValue,YValue = meshgrid(x,y)
T = lambdify((x,y), formula,'numpy')
ZValue = T(XValue,YValue )
Plot.contour(XValue, YValue, ZValue)

Related

I want to append 1st element of numpy array with 1st element in another numpy array and do this for all other elements

I have Three numpy arrays
X = [1,2,3,4,4,5,56,..,n]
Y = [1,2,344,4,4,4,..,n]
Z = [1,2,244,24,445,64,..,n]
I want to make output like this
final_list = [(X1,Y1,Z1),(X2,Y2,Z2),(X3,Y3,Z3), ... (Xn,Yn,Zn)]
And then to check if Z in any of them is > some threshold
Pop it up all with its correspondence X and Y
Is there some suggestions please?
I tried
np.conctatenate
but no any good results.
Thanks a lot:)
A simple way with if else could be:
X = [1,2,3,4,4,5,56]
Y = [1,2,344,4,4,4,89]
Z = [1,2,244,24,445,64,89]
d=[]
for i in range(len(X)):
if Z[i]>thresh:
print("print something")
else:
d.append([X[i],Y[i],Z[i]])
print(d)
if you check if z>thresh at the time of creating the list there is no need of poping those items later.
One way with just list comprehension:
out = [(x,y,z) for x,y,z in zip(X,Y,Z) if z<threshold]
With numpy you can do something like this:
xyz = np.array([X,Y,Z])
under_thresh = xyz[xyz[-1]<threshold]

How to select corresponding value in two data sets using matlab?

I have a two datasets (x,y) in in table:
x = [4.14;5.07;3.61;4.07;3.68;4.13;3.95;3.88;5.41;6.14]
y = [69.78;173.07;19.28;32.88;15.87;53.73;41.69;35.14;228.08;267.11];
tb = table(x,y)
edges = linspace(30, 0, 61);
Based on this I have written following program
for k = 1:length(x)
New(k) = find(x(k)>edges,1,'last');
end
I want to see datasets y which satisfying condition of of above x values.

Polyline() - change colour with an array value

I'm trying to create a very simple example of a for steps in [] loop using a Polyline() inside an IronPython WPF application. Each iteration of the loop should draw a different colour however Brushes implements a set of predefined System.Windows.Media.SolidColorBrush objects. I can't work out how to swap Red for my steps variable.
def polylineShape(self):
x = self.myCanvas.Width/2
y = self.myCanvas.Height/2
polyline = Polyline()
polyline.StrokeThickness = 5
for steps in ['Red','Blue','Green','Black']:
x = x
y = x
polyline.Points.Add(Point(x,y))
x = x + 40
polyline.Points.Add(Point(x,y))
polyline.Stroke = Brushes.Red #change colour on iteration
self.myCanvas.Children.Add(polyline)
I created a solution with some trial and error, I couldn't work out how to pass colours directly to the Brushes type.
def polylineShape(self):
x = 0
y = 0
for steps in [Brushes.SteelBlue, Brushes.DarkOrange, Brushes.DarkSeaGreen, Brushes.Honeydew]:
polyline = Polyline()
polyline.StrokeThickness = self.myCanvas.Height/4
x = 0
y = y + self.myCanvas.Height/4
polyline.Points.Add(Point(x,y))
x = self.myCanvas.Width
polyline.Points.Add(Point(x,y))
polyline.Stroke = steps
self.myCanvas.Children.Add(polyline)

MATLAB: search for elements in an array matching multidimensional condition

I have a column vector (V1) of real numbers like:
123.2100
125.1290
...
954.2190
If I add, let's say, a number 1 to each row in this vector, I will get (V2):
124.2100
126.1290
...
955.2190
I need to find out how many elements from V2 are inside some error-window created from V1. For example the error-window = 0.1 (but in my case every element in V1 has it's own error window):
123.1100 123.3100
125.0290 125.2290
...
954.1190 954.3190
I can create some code like this:
% x - my vector
% ppm - a variable responsible for error-window
window = [(1-(ppm/1000000))*x, (1+(ppm/1000000))*x]; % - error-window
mdiff = 1:0.001:20; % the numbers I will iteratively add to x
% (like the number 1 in the example)
cdiff = zeros(length(mdiff),1); % a vector that will contain counts of elements
% corresponding to different mdiff temp = 0;
for i = 1:length(mdiff)
for j = 1:size(window,1)
xx = x + mdiff(i);
indxx = find( xx => window(j,1) & xx <= window(j,2) );
if any(indxx)
temp = temp + length(indxx); %edited
end
end
cdiff(i) = temp;
temp = 0;
end
So, at the end cdiff will contain all the counts corresponding to mdiff. The only thing, I would like to make the code faster. Or is there a way to avoid using the second loop (with j)? I mean to directly use a multidimensional condition.
EDIT
I decided to simpify the code like this (thanking to the feedback I got here):
% x - my vector
% ppm - a variable responsible for error-window
window = [(1-(ppm/1000000))*x, (1+(ppm/1000000))*x]; % - error-window
mdiff = 1:0.001:20; % the numbers I will iteratively add to x
% (like the number 1 in the example)
cdiff = zeros(length(mdiff),1); % a vector that will contain counts of elements
% corresponding to different mdiff temp = 0;
for i = 1:length(mdiff)
xx = x + mdiff(i);
cdiff(i) = sum(sum(bsxfun(#and,bsxfun(#ge,xx,window(:,1)'),bsxfun(#le,xx,window(:,2)'))));
end
In this case the code works faster and seems properly
add = 1; %// how much to add
error = .1; %// maximum allowed error
V2 = V1 + add; %// build V2
ind = sum(abs(bsxfun(#minus, V1(:).', V2(:)))<error)>1; %'// index of elements
%// of V1 satisfying the maximum error condition. ">1" is used to because each
%// element is at least equal to itself
count = nnz(ind);
Think this might work for you -
%%// Input data
V1 = 52+rand(4,1)
V2 = V1+1;
t= 0.1;
low_bd = any(abs(bsxfun(#minus,V2,[V1-t]'))<t,2); %%//'
up_bd = any(abs(bsxfun(#minus,V2,[V1+t]'))<t,2); %%//'
count = nnz( low_bd | up_bd )
One could also write it as -
diff_map = abs(bsxfun(#minus,[V1-t V1+t],permute(V2,[3 2 1])));
count = nnz(any(any(diff_map<t,2),1))
Edit 1:
low_bd = any(abs(bsxfun(#minus,V2,window(:,1)'))<t,2); %%//'
up_bd = any(abs(bsxfun(#minus,V2,window(:,2)'))<t,2); %%//'
count = nnz( low_bd | up_bd )
Edit 2: Vectorized form for the edited code
t1 = bsxfun(#plus,x,mdiff);
d1 = bsxfun(#ge,t1,permute(window(:,1),[3 2 1]));
d2 = bsxfun(#le,t1,permute(window(:,2),[3 2 1]));
t2 = d1.*d2;
cdiff_vect = max(sum(t2,3),[],1)';

Trouble datamining with Django

I've set up a Django project in which i create random points. These random points are stored in a database(sqlite) (i can see them via the admin website and change the values, so this works).
If i the write a script i can access the points and print them in a plot. See code below.
But if I then want to mine these points to sort them or only plot a selection of the dataset i seem to have trouble. If i readout the values they are not connected anymore and sorting x would mix up the point set.
Is there a way to sort the data set to a minimum value of in this case X and the sort the values and print the set? (keep all x, y, z and name value of the point intact?) (see answer below, point in Point3D.objects.all().order_by('x'):)
If i now want to to have the values of x between x = 12 and x = 30? how can i add this extra filter?
My code is as follows:
models.py:
class Point3D(models.Model):
name = models.CharField(max_length = 10)
x = models.DecimalField(max_digits=5, decimal_places=2)
y = models.DecimalField(max_digits=5, decimal_places=2)
z = models.DecimalField(max_digits=5, decimal_places=2)
generate the points:
from books.models import Point3D
def points():
for i in range(20):
x = random.randint(0,100)
y = random.randint(0,100)
z = random.randint(0,100)
p = Point3D(name = x , x = x ,y = y,z = z)
# print 'test'
p.save()
#
points()
in views.py:
def ThreeGraphs(request):
fig = Figure()
fig.suptitle('2D-punten')
ax = fig.add_subplot(111)
for point in Point3D.objects.all():
print point
name = int(point.name)
xs = int(point.x)
ys = int(point.y)
zs = int(point.z)
print (xs, ys, zs)
ax.plot(xs, ys, 'bo' )
HttpResponse(mimetype="image/png")
FigureCanvas(fig)
fig.savefig('template/images/testing.png')
picture = "testing.png"
return render_to_response('Test.html', {'picture': picture}, RequestContext(request))
Hope anyone knows how to solve my trouble.
Thanks a lot!
Tijl
You need to to this:
for point in Point3D.objects.all().order_by('x'):
This will return the points in sorted order by the 'x' field. You can say order_by('-x') to reverse the sort order.

Resources