Using dataset change different input while solving simultaneous equations in R - dataframe

I have a certain dataset in R language, let's say
A<- data.frame(1:5)
B<- data.frame(1:5)
C<- data.frame(1:5)
D<- data.frame(1:5)
and 4 equation with 4 knowns X1, X2, X3 and X4
equ1<-A*(x[3]+x[4]+x[1])-x[2]*(x[3]+x[4]+x[1]-A)
equ2<-B*(x[2]+x[4]+x[1])-x[3]*(x[2]+x[4]+x[1]-B)
equ3<-C*(x[2]+x[3]+x[1])-x[4]*(x[2]+x[3]+x[1]-C)
equ4<-D*(x[2]+x[3]+x[4])-x[1]*(x[2]+x[3]+x[4]-D)
where each row in columns A, B, C, and D should go through this nonlinear equation to simultaneously solve, and output which will be X1, X2, X3, and X4 should be in a different column.
For A=21, B=223, C=232, and D=432
This code is working but not for the data frame.
library("nleqslv")
A<- data.frame(1:5)
B<- data.frame(1:5)
C<- data.frame(1:5)
D<- data.frame(1:5)
fn <- function(x[i])
x<-as.data.frame(x)
{
equ1<-A*(x[3]+x[4]+x[1])-x[2]*(x[3]+x[4]+x[1]-A)
equ2<-B*(x[2]+x[4]+x[1])-x[3]*(x[2]+x[4]+x[1]-B)
equ3<-C*(x[2]+x[3]+x[1])-x[4]*(x[2]+x[3]+x[1]-C)
equ4<-D*(x[2]+x[3]+x[4])-x[1]*(x[2]+x[3]+x[4]-D)
return(c(equ1,equ2,equ3,equ4))
}
}
sol=nleqslv(c(A, B,C,D),FUN=fn)
v<-sol$x[1]
x<-sol$x[2]
y<-sol$x[3]
z<-sol$x[4]
v
x
y
z
Waitng for you guys to provide me a solution.

Related

How can I assign a variable with the value of another variable in Kotlin?

In Code A, the parameters x1 and x2 use the same vaule, it works well.
I think I can improve Code A, so I write Code B, but it failed.
How can I assign x2 with the value of x1 ?
Code A
val stepWidth = step * index
it.drawChildnAxis(
x1 = stepWidth.toX, y1 = 0f.toY,
x2 = stepWidth.toX, y2 = yAxisLength.toY
)
fun Canvas.drawChildnAxis(x1:Float, y1:Float,x2:Float,y2:Float){
drawLine(
Offset(x = x1, y = y1),
Offset(x = x2, y = y2),
paintTableAxisChild
)
}
Code B
it.drawChildnAxis(
x1 = step * index.toX, y1 = 0f.toY,
x2 = x1, y2 = yAxisLength.toY
)
//The same
The x1 = ..., x2 = ... etc in your code are not actually assignment statements! They are named arguments.
There is no variable x1, x2 etc that becomes suddenly in scope at the function call, allowing you to assign values to it. This is just a bit of a syntax that lets you say the names of your parameters to make your code more readable, and sometimes resolve overload resolution ambiguities.
The syntax just so happened to be designed to look similar to assignments, making the left hand side look like a new variable just got declared. Would you still have this confusion if the syntax used : instead of =?
it.drawChildnAxis(
x1: stepWidth.toX, y1: 0f.toY,
x2: stepWidth.toX, y2: yAxisLength.toY
)
So x2 = x1 doesn't make sense - there is no such variable as x1 at that position. x1 is only the name of a parameter, which is only in scope when you are inside drawChildnAxis.
If you want to avoid repetition, just create a new variable yourself!
val x = stepWidth.toX
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
If you don't want x to be accessible afterwards, use a scope function:
stepWidth.toX.let { x ->
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
}
All of this is of course assuming that toX doesn't have side effects - calling its getter twice on the same thing gives the same value.

probability of sample of distribution

I am trying to generate a sample of 100 scenarios (X, Y) where both X and Y are normally distributed X=N(50,5^2), Y=N(30,2^2) and X and Y are correlated Cov(X,Y)=0.4.
I have been able to generate 100 scenarios with the Cholesky decomposition:
# We do a Cholesky decomposition to generate correlated scenarios
nScenarios = 10
Σ = [25 0.4; 0.4 4]
μ = [50, 30]
L = cholesky(Σ)
v = [rand(Normal(0, 1), nScenarios), rand(Normal(0, 1), nScenarios)]
X = reshape(zeros(nScenarios),1,nScenarios)
Y = reshape(zeros(nScenarios),1,nScenarios)
for i = 1:nScenarios
X[1, i] = sum(L.U[1, j] *v[j][i] for j = 1:nBreadTypes) + μ[1]
Y[1, i] = sum(L.U[2, j] *v[j][i] for j = 1:nBreadTypes) + μ[2]
end
However I need the probability of each scenario, i.e P(X=k and Y=p). My question would be, how can we get a sample of a certain distribution with the probability of each scenario?
Following the BatWannaBe explanation, normally I would do it like this:
julia> using Distributions
julia> d = MvNormal([50.0, 30.0], [25.0 0.4; 0.4 4.0])
FullNormal(
dim: 2
μ: [50.0, 30.0]
Σ: [25.0 0.4; 0.4 4.0]
)
julia> point = rand(d)
2-element Vector{Float64}:
52.807189619051485
32.693811008760676
julia> pdf(d, point)
0.0056519503173830515

How to plot two different color scales for two geom_points from two different dataframes in ggplot2?

I am trying to plot two datasets on the same graph. Both data are plotted using geom_point, and I want to separately represent the sizes and color by the z values.
x <- c(2,3,4,5)
y <- c(1.1,1.2,1.3,1.4)
z <- c(1,2,2,3)
x3 <- c(4,5,6,7)
y3 <- c(3.1,3.2,3.3,3.2)
z3<- c(1,2,3,4)
p1 <- data.frame(x=x,y=y,z=z)
p3 <- data.frame(x=x3,y=y3,z=z3)
s <- ggplot()+
geom_point(data= p1, aes(x=x,y=y, color=z, size=z))+
geom_point(data=p3, aes(x3,y=y3, color=z, size=z3))
How to I get continuous scale of colors and sizes separately to both geom_point? For example, z is scale_colour_gradient(low = "black", high = "red") and z3 is scale_colour_gradient(low = "light blue", high = "purple"). Similarly for sizes.
Thank you!
One of the easy ways would be with the ggnewscale package:
library(ggplot2)
library(ggnewscale)
x <- c(2,3,4,5)
y <- c(1.1,1.2,1.3,1.4)
z <- c(1,2,2,3)
x3 <- c(4,5,6,7)
y3 <- c(3.1,3.2,3.3,3.2)
z3<- c(1,2,3,4)
p1 <- data.frame(x=x,y=y,z=z)
p3 <- data.frame(x=x3,y=y3,z=z3)
s <- ggplot()+
geom_point(data= p1, aes(x=x,y=y, color=z, size=z))+
scale_colour_gradient(low = "black", high = "red") +
new_scale_colour() + # Define scales before initiating a new one
scale_size() +
new_scale("size") +
geom_point(data=p3, aes(x3,y=y3, color=z, size=z3)) +
scale_colour_gradient(low = "dodgerblue", high = "purple") +
scale_size()
s
Created on 2020-05-28 by the reprex package (v0.3.0)

Why is my naive line drawing algorithm faster than Bresenham

I implemented both the naive line drawing algorithm and bresenham algorithm. When I run the program with a 1000 lines, the naive line drawing algorithm is faster than the bresenham algorithm. Could anyone explain why?
Here is my code for both methods
def simpleLine(x1, y1, x2, y2):
dy = y2-y1;
dx = x2-x1;
x = x1
m = dy/dx;
b = y1-m*x1;
if(x1>x2):
x1,x2 = x2,x1
x=x1
while(x<=x2):
y=m*x+b;
PutPixle(win,x,round(y));
x=x+1
'
def BresenhamLine(x1, y1, x2, y2):
dx = abs(x2 - x1)
dy = abs(y2 - y1)
p = 2 * dy - dx
duady = 2 * dy
duadydx = 2 * (dy - dx)
x = x1
y = y1
xend = x2
if(x1 > x2):
x, y,xend = x2, y2,x1
while(x < xend):
PutPixle(win,x,y)
x =x+1
if(p<0):
p = p + 2*dy
else:
y = y-1 if y1>y2 else y+1
p = p+2*(dy-dx)
Bresenham's algorithm was invented for languages and machines with different performance characteristics than your python environment. In particular, low-level languages on systems where floating point math is much more expensive than integer math and branches.
In Python, your simple version is faster even though it uses floating point and rounding, because Python is slow and it executes fewer python operations per pixel. Any difference in speed between single integer or floating point operations is dwarfed by the cost of just doing python stuff.

Error code not working

Hello I am trying to implement the gate MiniALU but the howard simulator give me this error: "has no source pin". I would be happy if you can help me solve this.
my code-
CHIP MiniALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
zy, // zero the y input?
f; // compute out = x + y (if f == 1) or out = x & y (if == 0)
OUT
out[16]; // 16-bit output
PARTS:
// Zero the x input and y input
Mux16(a[0..15]=x, b[0..15]=false, sel=zx, out[0..15]=x1);
Mux16(a[0..15]=y, b[0..15]=false, sel=zy, out[0..15]=y1);
// Perform f
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
Mux16(a[0..15]=xandy, b[0..15]=xaddy, sel=f, out[0..15]=out);
}
You are connecting x2 and y2 to the inputs of And16 and Add16 but x2 and y2 are not defined anywhere.
You need to replace x2 and y2 with x1 and y1 in the connections to And16 and Add16.
Correct code:
Mux16(a=x, b=false, sel=zx, out=x1);
Mux16(a=y, b=false, sel=zy, out=y1);
And16(a=x1, b=y1, out=xandy);
Add16(a=x1, b=y1, out=xaddy);
Mux16(a=xandy, b=xaddy, sel=f, out=out);
Your problem was that you wrote y2 and x2 instead of y1 and x2.
Also, there is not need in [0..15]
You wrote:
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
instead of:
And16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xandy);
Add16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xaddy);