gap.barplot error bars not plotting - plotrix

I am desperately trying to find a way to plot custom error bars in gap.plot.
I used the code below but it is not working.
Thank you
b <- t(matrix( c(50.84033606,119.3277309,452.1008396,1971.0084,
2734.033609,115.1260502,239.0756299,569.7478983,1949.579829,
2607.983189),
nrow=5, ncol=2 ))
b=rbind(NA,d)
require(plotrix)
a <- gap.barplot(as.matrix(b),
gap=c(850,1600),
ytics=c(0,300,2000,2500),
ylab = "",
xlab = "",
xaxt='n',
pch=20,
las= 2,
xlim = c(0,18),
ylim = c(0,2750),
col=rep(c(grey.colors(2), 1), 6))
abline(h=seq(840,950), col="white")
axis.break(2, 850, style = "slash", brw = 0.060)
b2<- c(50.84033606,119.3277309,452.1008396,1971.0084,
2734.033609,115.1260502,239.0756299,569.7478983,1949.579829,
2607.983189)
se<- as.matrix(c(6.522762467,11.72524894,10.60456256,57.25512628,57.09610968,
5.388972823,16.50463808,24.69269495,64.07253048,129.4765469))
segments(a, d2 - se, a, d2 + se, lwd=1)
segments(a - 0.1, d2 - se, a + 0.1, d2 - se, lwd=1)
segments(a - 0.1, d2 + se, a + 0.1, d2 + se, lwd=1)

Related

Getting substrings in LuaU

the title might look very stupid but I really can't find out what the problem is, I have created a custom function to substring like the way in C++ because I like it:
local function SubStr(String, Start, Count)
return String:sub(Start, Start+(Count-1))
end
This is the code I am using, the substring function seems to work for the Bit Depth, Width and height, though when reading the colour values, it seems to offset a ton and I'm not sure what it is:
print("BitDepth", SubStr(Text, 1, 8))
print("Width", SubStr(Text, 9, 8))
print("Height", SubStr(Text, 17, 8))
Image["BitDepth"] = tonumber(SubStr(Text, 1, 8), 2)
Image["Width"] = tonumber(SubStr(Text, 9, 8), 2)
Image["Height"] = tonumber(SubStr(Text, 17, 8), 2)
for i = 1, Image["BitDepth"] do
Image["Colours"] = {};
Image["Colours"][i] = {};
print("RED", SubStr(Text, (24*i), 8))
print("BLUE", SubStr(Text, (24*i)+8, 8))
print("GREEN", SubStr(Text, (24*i)+16, 8))
print("ALPHA", SubStr(Text, (24*i)+24, 8))
The intended binary colour values for when i = 1, should all be 11111111
By the way this is reading the following binary string:
000000110000100000001000111111111111111111111111000000000000000000000000111111110000000000000000000000000000000011111111
I made some changes; test this out. The first offset start at 25 and second offset start from multiples of 32.
-- New v**strong text**ariables
offset = 25
count = 0; -- count is essentially (i - 1)
for i = 1, Image["BitDepth"] do
Image["Colours"] = {};
Image["Colours"][i] = {};
print(i)
-- first_offset + second_offset + third_offset
print("RED", SubStr(Text, offset + (32*count), 8))
print("BLUE", SubStr(Text, offset + (32*count)+8, 8))
print("GREEN", SubStr(Text, offset + (32*count)+16, 8))
print("ALPHA", SubStr(Text, offset + (32*count)+24, 8))
count = count + 1
end
3x8 is 24. So the next value starts at 25, not 24.

using previous/linked value in equation

It is possible to resolve this kind of equation :
const = [0x50, 0xe8, 0bcb, 0x9f, 0xa1]
data = IntVector('data', len(const))
for i in range(0, len(const)-1):
s.add(data[i] >= 32, data[i] <= 126)
s.add(data[i+1] >= 32, data[i+1] <= 126)
s.add(data[i] + data[i+1] == const[i]
or I misuse Z3 library ?
Yes. After fixing the obvious syntax errors, my Z3 reports it as unsatisfiable.
Supposing that 0bcb means something like 0xcb, then the list of constants is [80, 232, 203, 159, 161].
Then you ask for 5 variables, lets call them d0, d1, d2, d3, d4, which are each between 32 and 126. And where d0 + d1 == 80 (so d1 <= 48) and d1 + d2 == 232 (so d1 >= 106). This clearly contradicts, in which Z3 agrees. (Note that your constraints don't use the last element of const.)
Here is a slightly more pythonic version of the code which includes each of the 5 constants (with the second constant lowered to obtain a solvable system of constraints):
from Z3 import IntVector, Solver, sat
const = [0x50, 0xa9, 0xcb, 0x9f, 0xa1]
s = Solver()
data = IntVector('data', len(const)+1)
for d in data:
s.add(d >= 32, d <= 126)
for d0, d1, c0 in zip(data, data[1:], const):
s.add(d0 + d1 == c0)
result = s.check()
if result == sat:
print("Here is a solution: ")
m = s.model()
values = [m[d].as_long() for d in data]
print(values, " sums:", [hex(v0 + v1) for v0, v1 in zip(values, values[1:])])
elif result == unsat:
print("There is no solution")
else:
print("Z3 could not solve the constraints")
Output:
Here is a solution:
[37, 43, 126, 77, 82, 79] sums: ['0x50', '0xa9', '0xcb', '0x9f', '0xa1']

cspline in Maxima giving me a result which indicates an error in Maxima

I have the following cubic polynomial f(x)=x³ - 3 x² + x -5 for which the cubic spline should provide the exact same polynomial assuming the following data:
(-1, -10), (0,-5), (1, -6) with second derivative at the extremes f''(-1)=-12, f''(1)=0 (note that f''(x)=6x-6.)
Here the piece of code that I tried on:
/* polynomial to interpolate and data */
f(x) := x^3 - 3* x^2 + x - 5$
x0:-1$
x1:0$
x2:1$
y0:f(x0)$
y1:f(x1)$
y2:f(x2)$
p:[[x0,y0],[x1,y1],[x2,y2]]$
fpp(x) := diff(f(x),x,2);
fpp0 : at( fpp(x), [x=x0]);
fpp2 : at( fpp(x), [x=x2]);
/* here I call cspline with d1=fpp0 and dn=fpp2 */
load(interpol)$
cspline(p, d1=fpp0, dn=fpp2);
I expected the original polynomial (f(x)=x³ -3 x² + x -5) but I got the result:
(%o40) (-16*x^3-15*x^2+6*x-5)*charfun2(x,-inf,0)+(8*x^3-15*x^2+6*x-5)*charfun2(x,0,inf)
which does not agrees with the original polynomial.
Evenmore. Here is a test on the results provided by Maxima.
Code:
/* verification */
h11(x) := -16*x^3 - 15* x^2 + 6* x - 5;
h22(x) := 8* x^3 - 15*x^2 + 6* x - 5;
h11pp(x) := diff(h11(x), x, 2);
h11pp0: at( h11pp(x), [x=x0]);
h22pp(x) := diff(h22(x), x, 2);
h22pp2 : at(h22pp(x), [x=x2]);
which throws 66 and 18 as the boundary conditions, which should be instead -12 and 0.
Thanks.
It appears you've misinterpreted the arguments d1 and dn for cspline. As the description of cspline says, d1 and dn specify the first derivative for the spline at the endpoints, not the second derivative.
When I use the first derivative of f to specify the values for d1 and dn, I get the expected result:
(%i2) f(x) := x^3 - 3* x^2 + x - 5$
(%i3) [x0, x1, x2]: [-1, 0, 1] $
(%i4) [y0, y1, y2]: map (f, %);
(%o4) [- 10, - 5, - 6]
(%i5) p: [[x0, y0], [x1, y1], [x2, y2]];
(%o5) [[- 1, - 10], [0, - 5], [1, - 6]]
(%i6) load (interpol) $
(%i7) cspline (p, d1 = at(diff(f(x), x), x=x0), dn = at(diff(f(x), x), x=x2));
3 2
(%o7) (x - 3 x + x - 5) charfun2(x, minf, 0)
3 2
+ (x - 3 x + x - 5) charfun2(x, 0, inf)

Gradient Descent cost function explosion

I am writing this code for linear regression and trying Gradient Descent to minimize the RSS. The cost function seems to explode to infinity within 12 iterations. I know this is not supposed to happen. Maybe, I have used the wrong gradient function for RSS (can be seen in the function "grad()")?
NumberObservations=100
minVal=1
maxVal=20
X = np.random.uniform(minVal,maxVal,(NumberObservations,1))
e = np.random.normal(0, 1, (NumberObservations,1))
Y= 10 + 5*X + e
B = np.array([[0], [0]])
sum_y = sum(Y)
sum_x = sum(X)
sum_xy = sum(np.multiply(X, Y))
sum_x2 = sum(X*X)
alpha = 0.00001
iterations = 15
def cost_fun(X, Y, B):
b0 = B[0]
b1 = B[1]
s = (Y - (b0 + (b1*X)))**2
rss = sum(s)
return rss
def grad(X, Y, B):
print("B = " + str(B))
b0 = B[0]
b1 = B[1]
g0 = -2*(Y - b0 - (b1*X))
g1 = -2*((X*Y) - (b0*X) - (b1*X**2))
grad = np.concatenate((g0, g1), axis = 1)
return grad
def gradient_descent(X, Y, B, alpha, iterations):
cost_history = [0] * iterations
m = len(Y)
x0 = np.array(np.ones(m))
x0 = x0.reshape((100, 1))
X1 = np.concatenate((x0, X), axis = 1)
for iteration in range(iterations):
h = np.dot(X1, B)
h = h.reshape((100, 1))
loss = h - Y
g = grad(X, Y, B)
gradient = (np.dot(g.T, loss) / m)
B = B - alpha * gradient
cost = cost_fun(X, Y, B)
cost_history[iteration] = cost
print("Iteration %d | Cost: %f" % (iteration, cost))
print("-----------------------------------------------------------------------")
return B, cost_history
newB, cost_history = gradient_descent(X, Y, B, alpha, iterations)
# New Values of B
print(newB)
Please help.

Sage: Polynomial ring over finite field - inverting a polynomial non-prime

I'm trying to recreate the wiki's example procedure, available here:
https://en.wikipedia.org/wiki/NTRUEncrypt
I've run into an issue while attempting to invert the polynomials.
The SAGE code below seems to be working fine for the given p=3, which is a prime number.
However, the representation of the polynomial in the field generated by q=32 ends up wrong, because it behaves as if the modulus was 2.
Here's the code in play:
F = PolynomialRing(GF(32),'a')
a = F.gen()
Ring = F.quotient(a^11 - 1, 'x')
x = Ring.gen()
pollist = [-1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1]
fq = Ring(pollist)
print(fq)
print(fq^(-1))
The Ring is described as follows:
Univariate Quotient Polynomial Ring in x over Finite Field in z5 of size 2^5 with modulus a^11 + 1
And the result:
x^10 + x^9 + x^6 + x^4 + x^2 + x + 1
x^5 + x + 1
I've tried to replace the Finite Field with IntegerModRing(32), but the inversion ends up demanding a field, as implied by the message:
NotImplementedError: The base ring (=Ring of integers modulo 32) is not a field
Any suggestions as to how I could obtain the correct inverse of f (mod q) would be greatly appreciated.
GF(32) is the finite field with 32 elements, not the integers modulo 32. You must use Zmod(32) (or IntegerModRing(32), as you suggested) instead.
As you point out, Sage psychotically bans you from computing inverses in ℤ/32ℤ[a]/(a¹¹-1) because that is not a field, and not even a factorial ring. It can, however, compute those inverses when they exist, only you must ask more kindly:
sage: F.<a> = Zmod(32)[]
sage: fq = F([-1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1])
sage: print(fq)
31*a^10 + a^9 + a^6 + 31*a^4 + a^2 + a + 31
sage: print(fq.inverse_mod(a^11 - 1))
16*a^8 + 4*a^7 + 10*a^5 + 28*a^4 + 9*a^3 + 13*a^2 + 21*a + 1
Not ideal, admittedly.

Resources