Sage does not recognize a unit ideal - sage

I am trying to learn a bit of sage and I have tried playing with ideals in p-adic rings. But I don't understand how ideals work in polynomial rings. I have tried writing the following
A=Zp(5)
R.<x>=A[]
g1=(1+x)^(25)-1;
g2=6^(-25)*((1+x)^(25)-6^(25));
J=R.ideal(37,g1)
I=R.ideal(37,g2)
U=R.unit_ideal();
I==U
I==J
J==U
Since 37 is a 5-adic unit, I would expect the answer to be
True
True
True
because both I and J contain 37. But it is
False
False
False
What am I missing? I also apologise for not being able to understand how to TeX in stackoverflow.

Well, apparently Sage isn't checking that automatically.
print(U)
print(I)
print(J)
Principal ideal (1 + O(5^20)) of Univariate Polynomial Ring in x over 5-adic Ring with capped relative precision 20
Ideal (2 + 2*5 + 5^2 + O(5^20), (1 + 4*5^3 + 2*5^4 + 4*5^7 + 2*5^8 + 3*5^9 + 3*5^10 + 3*5^12 + 5^13 + 4*5^15 + 4*5^16 + 2*5^17 + 5^18 + 5^19 + O(5^20))*x^25 + (5^2 + 4*5^5 + 2*5^6 + 4*5^9 + 2*5^10 + 3*5^11 + 3*5^12 + 3*5^14 + 5^15 + 4*5^17 + 4*5^18 + 2*5^19 + O(5^20))*x^24 + (2*5^2 + 2*5^3 + 3*5^5 + 3*5^6 + 5^7 + 5^8 + 3*5^9 + 3*5^10 + 2*5^11 + 4*5^12 + 3*5^13 + 2*5^14 + 4*5^15 + 3*5^16 + 3*5^17 + 2*5^18 + O(5^20))*x^23 + (2*5^2 + 3*5^3 + 3*5^4 + 3*5^5 + 2*5^6 + 5^7 + 3*5^10 + 2*5^11 + 5^12 + 3*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 3*5^17 + 2*5^18 + 2*5^19 + O(5^20))*x^22 + (5^2 + 5^3 + 3*5^5 + 2*5^6 + 3*5^7 + 5^8 + 4*5^10 + 5^11 + 3*5^12 + 3*5^14 + 4*5^15 + 4*5^16 + 5^17 + 4*5^19 + O(5^20))*x^21 + (5 + 4*5^4 + 4*5^5 + 3*5^6 + 2*5^8 + 3*5^10 + 4*5^12 + 2*5^14 + 3*5^15 + 5^16 + 3*5^17 + 2*5^19 + O(5^20))*x^20 + (4*5^2 + 5^3 + 3*5^4 + 2*5^5 + 5^6 + 4*5^7 + 3*5^8 + 4*5^9 + 5^10 + 2*5^12 + 4*5^13 + 3*5^14 + 4*5^15 + 3*5^16 + 2*5^17 + 2*5^19 + O(5^20))*x^19 + (3*5^2 + 4*5^4 + 4*5^6 + 3*5^7 + 4*5^8 + 2*5^9 + 4*5^10 + 3*5^11 + 2*5^12 + 4*5^14 + 2*5^16 + O(5^20))*x^18 + (3*5^2 + 2*5^3 + 3*5^5 + 5^7 + 3*5^8 + 5^9 + 3*5^14 + 5^16 + 2*5^17 + 5^18 + 5^19 + O(5^20))*x^17 + (4*5^2 + 3*5^3 + 3*5^4 + 4*5^5 + 3*5^6 + 3*5^7 + 2*5^8 + 2*5^9 + 4*5^10 + 2*5^11 + 5^13 + 5^14 + 2*5^15 + 3*5^17 + 5^18 + 5^19 + O(5^20))*x^16 + (2*5 + 3*5^4 + 5^5 + 2*5^7 + 5^9 + 3*5^10 + 4*5^11 + 3*5^12 + 4*5^13 + 2*5^14 + 3*5^15 + 4*5^16 + 2*5^17 + 4*5^19 + O(5^20))*x^15 + (5^2 + 4*5^3 + 5^4 + 4*5^6 + 2*5^7 + 3*5^10 + 3*5^11 + 5^12 + 3*5^13 + 2*5^14 + 3*5^15 + 3*5^16 + 5^17 + 5^19 + O(5^20))*x^14 + (2*5^2 + 2*5^3 + 2*5^5 + 5^6 + 3*5^7 + 5^10 + 4*5^11 + 2*5^12 + 3*5^14 + 5^15 + 4*5^16 + 2*5^17 + 4*5^18 + 2*5^19 + O(5^20))*x^13 + (2*5^2 + 2*5^3 + 2*5^5 + 5^6 + 3*5^7 + 5^10 + 4*5^11 + 2*5^12 + 3*5^14 + 5^15 + 4*5^16 + 2*5^17 + 4*5^18 + 2*5^19 + O(5^20))*x^12 + (5^2 + 4*5^3 + 5^4 + 4*5^6 + 2*5^7 + 3*5^10 + 3*5^11 + 5^12 + 3*5^13 + 2*5^14 + 3*5^15 + 3*5^16 + 5^17 + 5^19 + O(5^20))*x^11 + (2*5 + 3*5^4 + 5^5 + 2*5^7 + 5^9 + 3*5^10 + 4*5^11 + 3*5^12 + 4*5^13 + 2*5^14 + 3*5^15 + 4*5^16 + 2*5^17 + 4*5^19 + O(5^20))*x^10 + (4*5^2 + 3*5^3 + 3*5^4 + 4*5^5 + 3*5^6 + 3*5^7 + 2*5^8 + 2*5^9 + 4*5^10 + 2*5^11 + 5^13 + 5^14 + 2*5^15 + 3*5^17 + 5^18 + 5^19 + O(5^20))*x^9 + (3*5^2 + 2*5^3 + 3*5^5 + 5^7 + 3*5^8 + 5^9 + 3*5^14 + 5^16 + 2*5^17 + 5^18 + 5^19 + O(5^20))*x^8 + (3*5^2 + 4*5^4 + 4*5^6 + 3*5^7 + 4*5^8 + 2*5^9 + 4*5^10 + 3*5^11 + 2*5^12 + 4*5^14 + 2*5^16 + O(5^20))*x^7 + (4*5^2 + 5^3 + 3*5^4 + 2*5^5 + 5^6 + 4*5^7 + 3*5^8 + 4*5^9 + 5^10 + 2*5^12 + 4*5^13 + 3*5^14 + 4*5^15 + 3*5^16 + 2*5^17 + 2*5^19 + O(5^20))*x^6 + (5 + 4*5^4 + 4*5^5 + 3*5^6 + 2*5^8 + 3*5^10 + 4*5^12 + 2*5^14 + 3*5^15 + 5^16 + 3*5^17 + 2*5^19 + O(5^20))*x^5 + (5^2 + 5^3 + 3*5^5 + 2*5^6 + 3*5^7 + 5^8 + 4*5^10 + 5^11 + 3*5^12 + 3*5^14 + 4*5^15 + 4*5^16 + 5^17 + 4*5^19 + O(5^20))*x^4 + (2*5^2 + 3*5^3 + 3*5^4 + 3*5^5 + 2*5^6 + 5^7 + 3*5^10 + 2*5^11 + 5^12 + 3*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 3*5^17 + 2*5^18 + 2*5^19 + O(5^20))*x^3 + (2*5^2 + 2*5^3 + 3*5^5 + 3*5^6 + 5^7 + 5^8 + 3*5^9 + 3*5^10 + 2*5^11 + 4*5^12 + 3*5^13 + 2*5^14 + 4*5^15 + 3*5^16 + 3*5^17 + 2*5^18 + O(5^20))*x^2 + (5^2 + 4*5^5 + 2*5^6 + 4*5^9 + 2*5^10 + 3*5^11 + 3*5^12 + 3*5^14 + 5^15 + 4*5^17 + 4*5^18 + 2*5^19 + O(5^20))*x + 4*5^3 + 2*5^4 + 4*5^7 + 2*5^8 + 3*5^9 + 3*5^10 + 3*5^12 + 5^13 + 4*5^15 + 4*5^16 + 2*5^17 + 5^18 + 5^19 + O(5^20)) of Univariate Polynomial Ring in x over 5-adic Ring with capped relative precision 20
Ideal (2 + 2*5 + 5^2 + O(5^20), (1 + O(5^20))*x^25 + (5^2 + O(5^20))*x^24 + (2*5^2 + 2*5^3 + O(5^20))*x^23 + (2*5^2 + 3*5^3 + 3*5^4 + O(5^20))*x^22 + (5^2 + 5^3 + 4*5^5 + O(5^20))*x^21 + (5 + 2*5^5 + 3*5^6 + O(5^20))*x^20 + (4*5^2 + 5^3 + 3*5^4 + 5^5 + 5^6 + 2*5^7 + O(5^20))*x^19 + (3*5^2 + 4*5^4 + 3*5^5 + 5^7 + 5^8 + O(5^20))*x^18 + (3*5^2 + 2*5^3 + 5^5 + 4*5^6 + 3*5^7 + 2*5^8 + O(5^20))*x^17 + (4*5^2 + 3*5^3 + 3*5^4 + 3*5^5 + 5^7 + 5^9 + O(5^20))*x^16 + (2*5 + 5^5 + 4*5^6 + 5^7 + 3*5^8 + 5^9 + O(5^20))*x^15 + (5^2 + 4*5^3 + 5^4 + 5^5 + 2*5^7 + 5^8 + 2*5^9 + O(5^20))*x^14 + (2*5^2 + 2*5^3 + 4*5^5 + 2*5^6 + 5^7 + 3*5^8 + 2*5^9 + O(5^20))*x^13 + (2*5^2 + 2*5^3 + 4*5^5 + 2*5^6 + 5^7 + 3*5^8 + 2*5^9 + O(5^20))*x^12 + (5^2 + 4*5^3 + 5^4 + 5^5 + 2*5^7 + 5^8 + 2*5^9 + O(5^20))*x^11 + (2*5 + 5^5 + 4*5^6 + 5^7 + 3*5^8 + 5^9 + O(5^20))*x^10 + (4*5^2 + 3*5^3 + 3*5^4 + 3*5^5 + 5^7 + 5^9 + O(5^20))*x^9 + (3*5^2 + 2*5^3 + 5^5 + 4*5^6 + 3*5^7 + 2*5^8 + O(5^20))*x^8 + (3*5^2 + 4*5^4 + 3*5^5 + 5^7 + 5^8 + O(5^20))*x^7 + (4*5^2 + 5^3 + 3*5^4 + 5^5 + 5^6 + 2*5^7 + O(5^20))*x^6 + (5 + 2*5^5 + 3*5^6 + O(5^20))*x^5 + (5^2 + 5^3 + 4*5^5 + O(5^20))*x^4 + (2*5^2 + 3*5^3 + 3*5^4 + O(5^20))*x^3 + (2*5^2 + 2*5^3 + O(5^20))*x^2 + (5^2 + O(5^20))*x + O(5^20)) of Univariate Polynomial Ring in x over 5-adic Ring with capped relative precision 20
Perhaps more troubling,
U>=I
False
However, I'm not an expert in the implementation of these rings. However, Sage does give
R(37).is_unit()
True
On the other hand,
K = R.ideal(A(37))
K==U
gives True but
L = R.ideal(1,g1)
L == U
gives False.
I feel like there must be something obvious I'm missing here, but if so maybe the documentation needs some work, so I've opened Trac 28692. If we're off base, I'm sure we'll hear - you may also wish to ask on ask.sagemath where a lot more devs hang out.

Related

Problem adding a trendline to a graph in R ggplot2

I tried to use geom_smooth(method = "lm") and it doesn't work...
percentage.no.work <- cleanData %>% group_by(AREA) %>%
summarise(percentage = mean(ESTIMATED.CITY.UNEMPLOYMENT))
ggplot() +
geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
geom_smooth(method = "lm") +
theme_minimal() + ggtitle("Percentage Estimated City Unemployment") +
ylab("Percentage")
You need to provide the aesthetics for the geom_smooth as well. Either by including it in the ggplot() or in the geom_smooth() :
ggplot() +
geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
geom_smooth(aes(x=AREA, y=percentage), method = "lm") +
theme_minimal() + ggtitle("Percentage Estimated City Unemployment") +
ylab("Percentage")
You can avoid repeating section of the code putting it in the ggplot()
ggplot(data=percentage.no.work, aes(x=AREA, y=percentage)) +
geom_point(alpha=0.6, color="purple", size=2) +
geom_smooth(method = "lm") +
theme_minimal() + ggtitle("Percentage Estimated City Unemployment") +
ylab("Percentage")

Using for loop to build 10 plots using ggplot() - R

I'm trying to build 10 different plots showing consumption choices (organic, non-organic) using ggplot(). I would like to use a loop to build these plots, instead of building them one by one, here is the original code when I tried to build it one by one:
a <- with(data, table(Banana_Choice))
p1 <- ggplot(as.data.frame(a), aes(factor(Banana_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = a), vjust= -0.3) + xlab("Banana") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p1
b <- with(data, table(Apple_Choice))
p2 <- ggplot(as.data.frame(b), aes(factor(Apple_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = b), vjust= -0.3) + xlab("Apple") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p2
c <- with(data, table(Tomato_Choice))
p3 <- ggplot(as.data.frame(c), aes(factor(Tomato_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = c), vjust= -0.3) + xlab("Tomato") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p3
d <- with(data, table(Cucumber_Choice))
p4 <- ggplot(as.data.frame(d), aes(factor(Cucumber_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = d), vjust= -0.3) + xlab("Cucumber") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p4
e <- with(data, table(Broccoli_Choice))
p5 <- ggplot(as.data.frame(e), aes(factor(Broccoli_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = e), vjust= -0.3) + xlab("Broccoli") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p5
f <- with(data, table(Milk_Choice))
p6 <- ggplot(as.data.frame(f), aes(factor(Milk_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = f), vjust= -0.3) + xlab("Milk") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p6
g <- with(data, table(Cheese_Choice))
p7 <- ggplot(as.data.frame(g), aes(factor(Cheese_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = g), vjust= -0.3) + xlab("Cheese") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p7
h <- with(data, table(Wine_Choice))
p8 <- ggplot(as.data.frame(h), aes(factor(Wine_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = h), vjust= -0.3) + xlab("Wine") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p8
i <- with(data, table(MilkChoco_Choice))
p9 <- ggplot(as.data.frame(i), aes(factor(MilkChoco_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = i), vjust= -0.3) + xlab("Milk Chocolate") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p9
j <- with(data, table(DarkChoco_Choice))
p10 <- ggplot(as.data.frame(j), aes(factor(DarkChoco_Choice), Freq)) +
geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
geom_text(aes(label = j), vjust= -0.3) + xlab("Dark Chocolate") + ylab("Frequency (count)")+
theme(axis.ticks = element_blank(),
text=element_text(size=11))+ylim(0,100)
p10
As you can see, this is very slow. How do I build these plots using a for loop?
you can try making your data into a longer format using tidyr::pivot_longer(), and then use facet_wrap() to generate all the plots at once.

R add line breaks to expressions

I have very long expressions stored in a .txt file. By copying this epxression into R, it is represented in one line. When I try to run it, it doesn't work and errors like "unexpected symbol" or "unexpected numeric constant" appear. However, if I insert line breaks at arbitrary places, e.g. after every 3000 characters, it works smoothly.
Does anyone know how to handle this problem? In particular, is it possible to make these line breaks automatically or are there other possible solutions?
alpha <- 0.025; d1 <- 0.027; d2 <- 0.037; mu1 <- 0.024; mu2 <- 0.026; Z1 <- 0.6; Z2 <- 0.7; rho11 <- 0.076;
rho12 <- 0.074; rho21 <- 0.073; rho22 <- 0.072; S11 <- 0.086; S12 <- 0.087; S21 <- 0.084; S22 <- 0.083; u <- 2
exp(mu1*(mu1*u*d1^2 + d2*mu2*u*d1) - log(1 - 2*S22*d2^2*u - 2*S12*d1*d2*u - 2*S21*d1*d2*u - 2*S11*d1^2*u)/2 + mu2*(mu2*u*d2^2 + d1*mu1*u*d2) - (S21*((2*(S12*u*d2^2 + S11*d1*u*d2)*(d1^2*mu1*u + alpha*d1*u + d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((d2^2*mu2*u + alpha*d2*u + d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((2*(S21*u*d1^2 + S22*d2*u*d1)*(d2^2*mu2*u + alpha*d2*u + d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((d1^2*mu1*u + alpha*d1*u + d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))*(2*d1^2*mu1*u + 2*alpha*d1*u + 2*d1*d2*mu2*u) - (S22*((2*(S12*u*d2^2 + S11*d1*u*d2)*(d1^2*mu1*u + alpha*d1*u + d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((d2^2*mu2*u + alpha*d2*u + d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((2*(S21*u*d1^2 + S22*d2*u*d1)*(d2^2*mu2*u + alpha*d2*u + d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((d1^2*mu1*u + alpha*d1*u + d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))*(2*d2^2*mu2*u + 2*alpha*d2*u + 2*d1*d2*mu1*u) + Z1*(rho11*(2*d1^2*mu1*u + 2*alpha*d1*u + 2*d1*d2*mu2*u) - rho21*(d2^2*u*(S22*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho11*(d1^2*u*(S21*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + rho21*(2*d2^2*mu2*u + 2*alpha*d2*u + 2*d1*d2*mu1*u) + 2*alpha*d1*u) + Z2*(rho12*(2*d1^2*mu1*u + 2*alpha*d1*u + 2*d1*d2*mu2*u) - rho22*(d2^2*u*(S22*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho12*(d1^2*u*(S21*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((2*(S12*u*d2^2 + S11*d1*u*d2)*(4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((2*(S21*u*d1^2 + S22*d2*u*d1)*(4*d2^2*mu2*u + 4*alpha*d2*u + 4*d1*d2*mu1*u))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - ((4*d1^2*mu1*u + 4*alpha*d1*u + 4*d1*d2*mu2*u)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + rho22*(2*d2^2*mu2*u + 2*alpha*d2*u + 2*d1*d2*mu1*u) + 2*alpha*d2*u) + 2*alpha^2*u + Z1*(Z1*(rho11*(rho11*u*d1^2 + d2*rho21*u*d1) + rho21*(rho21*u*d2^2 + d1*rho11*u*d2) - rho11*(d1^2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho21*(d2^2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + d1^2*u) + Z2*(rho11*(rho12*u*d1^2 + d2*rho22*u*d1) + rho21*(rho22*u*d2^2 + d1*rho12*u*d2) - rho11*(d1^2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho21*(d2^2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + d1*d2*u)) + Z2*(Z2*(rho12*(rho12*u*d1^2 + d2*rho22*u*d1) + rho22*(rho22*u*d2^2 + d1*rho12*u*d2) - rho12*(d1^2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho22*(d2^2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho12*u*d1^2 + d2*rho22*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho22*u*d2^2 + d1*rho12*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho22*u*d2^2 + d1*rho12*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho12*u*d1^2 + d2*rho22*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + d2^2*u) + Z1*(rho12*(rho11*u*d1^2 + d2*rho21*u*d1) + rho22*(rho21*u*d2^2 + d1*rho11*u*d2) - rho12*(d1^2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) - rho22*(d2^2*u*(S22*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S12*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1))) + d1*d2*u*(S21*((4*(S12*u*d2^2 + S11*d1*u*d2)*(rho11*u*d1^2 + d2*rho21*u*d1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho21*u*d2^2 + d1*rho11*u*d2)*(2*S11*u*d1^2 + 2*S12*d2*u*d1 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)) + S11*((4*(S21*u*d1^2 + S22*d2*u*d1)*(rho21*u*d2^2 + d1*rho11*u*d2))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1) - (2*(rho11*u*d1^2 + d2*rho21*u*d1)*(2*S22*u*d2^2 + 2*S21*d1*u*d2 - 1))/(2*S11*d1^2*u + 2*S22*d2^2*u + 2*S12*d1*d2*u + 2*S21*d1*d2*u - 1)))) + d1*d2*u)) + 2*alpha*d1*mu1*u + 2*alpha*d2*mu2*u)
The cause of this is described well in the answer to this question
In your case, since you've got the expression in a file, you can source() the file from R, rather than copying and pasting. Either you need to add an assignment in your text file, or you can do:
source("filename.txt")$value
to get the result of the computation
You mention in the comments that you want to be able to use this in an optimisation - a simple example over one dimension to find an alpha that minimise the expression would be:
f <- function(alpha){source("o:\\test.txt")$value}
optim(1, f, lower=0.5, upper=1.5, method="L-BFGS-B")
#$par
#[1] 1
#
#$value
#[1] 1.011915
#
#$counts
#function gradient
# 1 1
#
#$convergence
#[1] 0
#
#$message
#[1] "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL"

how to overlay geom_rect and geom_boxplot in ggplot?

I am trying to use ggplot in order to overlay geom_rect under some boxplots.
I want the grey rectangle will be behind the box plots but I can't do it for some reason.
This is the code that I'm using:
ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) +
geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) +
geom_hline(yintercept = avg, color="red") +
#geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") +
#geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") +
geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
theme(panel.background = element_blank()) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')
The ggplot package draws the geom layers in the order that you declare them. If you want the geom_rect layer in the back, put it before the other layers in the code:
ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) +
geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) +
geom_hline(yintercept = avg, color="red") +
#geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") +
#geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") +
theme(panel.background = element_blank()) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')

Issue displaying legend with ggplot2, nothing displaying ... legend items have no correspnsing variable in df

I am plotting a series of lines using ggplot and shading between them. There are 10 lines, but only 5 shaded regions. I want the legend for the 5 shaded regions. At the moment no legend is showing. I thought the line
scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
would add a manual colour bar with the shaded region, but nothing shows. The full plot code is below. Thanks
Exposure_by_Month_10perc=Exposure_by_Month[,c(1,seq(3,length(monthQuant[1,])+1,4))]
dfm <- melt(Exposure_by_Month_10perc, id.vars = "Date")
dfm$value=dfm$value/1000000
colnames(dfm)=c("Date","Confidence Interval","value")
ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3]), fill = "Darkolivegreen1", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3]), fill = "Darkolivegreen2", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3]), fill = "Darkolivegreen3", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3]), fill = "Darkolivegreen4", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3]), fill = "Darkolivegreen", alpha = .5) +
scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "XXXX TITLE", subtitle = "XXX SUBTITLE") +
xlab("Month") +
ylab("Value, $M") +
scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
###why no legend
> head(Exposure_by_Month_10perc)
# A tibble: 6 x 11
Date `0.05` `0.15` `0.25` `0.35` `0.45` `0.55` `0.65` `0.75` `0.85` `0.95`
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2018-10-01 -1.44e6 -2.58e6 -3.42e6 -4.27e6 -5.15e6 -6.01e6 -7.01e6 -8.11e6 -9.51e6 -1.21e7
2 2018-11-01 -5.36e4 -1.42e6 -2.35e6 -3.35e6 -4.40e6 -5.67e6 -7.09e6 -8.66e6 -1.07e7 -1.50e7
3 2018-12-01 6.63e5 -7.65e5 -1.87e6 -3.05e6 -4.32e6 -5.82e6 -7.62e6 -9.68e6 -1.25e7 -1.87e7
4 2019-01-01 8.34e5 -9.67e5 -1.90e6 -2.68e6 -3.64e6 -4.73e6 -5.59e6 -6.94e6 -8.66e6 -1.19e7
5 2019-02-01 1.16e6 -4.70e5 -1.52e6 -2.13e6 -3.10e6 -4.15e6 -5.13e6 -6.53e6 -8.31e6 -1.20e7
6 2019-03-01 1.88e6 -6.02e3 -1.40e6 -2.10e6 -3.26e6 -4.49e6 -5.72e6 -7.46e6 -9.74e6 -1.38e7
[Plot without legend]1
Try this:
scale_color_manual(guide="legend", labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
This was solved by placing the fill=XXX inside the aes bracket of the geom_ribbon functions, as per code below:
ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3], fill = "Darkolivegreen1"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3], fill = "Darkolivegreen2"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3], fill = "Darkolivegreen3"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3], fill = "Darkolivegreen4"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3], fill = "Darkolivegreen"), alpha = .5) +
scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Tullow Hedging Portfolio", subtitle = "Value at Risk (VAR) Analysis") +
xlab("Month") +
ylab("Value at Risk, $M") +
scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
scale_fill_manual(name="Confidence Interval",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))

Resources