I got three equations.
1) 0+0=0
2) 1+0=1
3) 1+1=10
I have to calculate this with the help of the three equations
i) 1+0+0+0=?
ii) 0+1+0=?
iii) 1+1+0=?
But I don't know how to start
This is obviously homework so I'm not going to give you the answers. Instead, I'll leave you a few hints.
0 + 1 + 0 = (0 + 1) + 0 = (1 + 0) + 0
1 + 1 + 0 = (1 + 1) + 0 = 10 + 0
Binary and Decimal represent the same numbers just in a different way.
You can check your answers using the following mapping from decimal to binary.
Decimal -> Binary
0 = 0 + 0 + 0 -> 0
1 = 0 + 0 + 2^0 -> 1
2 = 0 + 2^1 + 0 -> 10
3 = 0 + 2^1 + 2^0 -> 11
4 = 2^2 + 0 + 0 -> 100
5 = 2^2 + 0 + 2^0 -> 101
6 = 2^2 + 2^1 + 0 -> 110
7 = 2^2 + 2^1 + 2^0 -> 111
and so on.
Binary is just another way of writing the same old same old numbers. What is 1+1 in a normal amount number system? When you add numbers together you can be transitive normally:
(a+b)+c = a+(b+c)
and
a+b = b+a
Related
I have 3 questions relating to the compare_df() function within the compareDF CRAN package.
I have two data frames with identical structures but different contents (this_week and last_week):
this_week
Week A B C
1 1 0 0 0
2 2 0 1 0
3 3 0 1 0
4 4 2 1 0
5 5 2 0 0
last_week
Week A B C
1 1 0 0 0
2 2 0 0 0
3 3 0 0 1
4 4 3 0 0
5 5 0 0 0
I am using compare_df(this_week, last_week, group_col = "Week") to compare these two data frames. Specifically, I am interested in the second of the compare_df() function outputs which gives cell-level comparisons.
The output shows which cells have increased from one week to the next:
weeks_compared <- compare_df(this_week, last_week, group_col = "Week")
weeks_compared
$comparison_df
Week chng_type A B C
1 2 + 0 1 0
2 2 - 0 0 0
3 3 + 0 1 0
4 3 - 0 0 1
5 4 + 2 1 0
6 4 - 3 0 0
7 5 + 2 0 0
8 5 - 0 0 0
$comparison_table_diff
Week chng_type A B C
1 = + = + =
2 = - = - =
3 = + = + +
4 = - = - -
5 = + + + =
6 = - - - =
7 = + + = =
8 = - - = =
Interestingly, row 5 and 6 do not provide the comparison results that I would expect. I would expect:
row 5, column 3 ("A") of the second dataframe ($comparison_table_diff) to be "-"
row 6, column 3 ("A") to be "+".
However, it is actually the opposite way around:
$comparison_df
Week chng_type A B C
5 4 + 2 1 0
6 4 - 3 0 0
$comparison_table_diff
Week chng_type A B C
5 = + + + =
6 = - - - =
1) Does anyone know why this happens?
In addition, I do not know how to use this output further. My aims are:
2) To update the old data which has increased in last_week
3) To add an asterisk to the last_week data which has increased (in columns "B" and "C" only)
I have not found anything related to actually using the compare_df() outputs on Stack Overflow other than to simply paste these tables, which isn't sufficient for my task.
I wondered if anyone has done anything similar and/or could share some ideas of how I might go about reaching these two aims. Alternatively, would be interested to know if there is a better package to use/workaround for this task. And of course, let me know if there is any further information that's required.
Thanks in advance for any help you can provide!
How would the recursive sequence a(n)=-a(n-1)+n-1 be solved?
I tried forward and backward iterations but haven't been able to get a explicit solution for a(n).
Your first step should be to write out a result table
f(n)=x
n | x
-----
0 | 7
1 | -7 (-7 + 1 - 1)
2 | 8 ( 7 + 2 - 1)
3 | -6 (-8 + 3 - 1)
4 | 9 ( 6 + 4 - 1)
5 | -5 (-9 + 5 - 1)
6 | 10 ( 5 + 6 - 1)
7 | -4 (-10 + 7 - 1)
8 | 11 ( 4 + 8 - 1)
9 | -3 (-11 + 9 - 1)
You should see a pattern emerging. Each pair of solutions [(0, 1), (2, 3), (4, 5), ...] have a difference of 14, starting with (7, -7) and incrementing one every two points of n. We can generalize this:
f(0) = 7
f(n) = 7 + k - 14 * b
where k is the increment value (each 1 k per 2 n)
b is 1 when n is odd, else 0.
Now we just have to define k and b in terms of n. k shouldn't be too hard, let's see:
n | k
0 | 0
1 | 0
2 | 1
3 | 1
Does that remind you of anything? That's a floored div2.
7 + (n // 2) - 14 * b
Now for b
n | b
0 | 0
1 | 1
2 | 0
3 | 1
That looks like mod 2 to me! Modulo is the remainder of a division problem, and is a great way to check if a number is even or odd. We're looking for the plain modulo, too, since we want b==1 when n is odd and vice versa.
f(0) = 7
f(n) = 7 + (n // 2) - 14 * (n%2)
where (//) is the floor division function
(%) is the modulo function
Now we can put that all together in a function. In Go this is:
func f(n int) int {
return 7 + n/2 - 14 * (n % 2)
}
In Python it's
def f(n):
return 7 + n//2 - 14 * (n%2)
In Haskell we've got
f :: Int -> Int
f n = 7 + n `div` 2 - 14 * (n `mod` 2)
or, since Haskell implements recursion exceedingly well, simply...
f :: Int -> Int
f 0 = 7
f n = f (n-1) + n - 1
This might sound like a School assignment but it is not!
I have made a recursive function returning a value from the Fibonacci Sequence.
let rec FoneFive n =
match n with
| 1 | 2 -> 1
| n -> FoneFive(n-1) + FoneFive(n-2)
printfn "%A" (FoneFive 6)
What is going on in this recursive function? FoneFive 6 gives 8 as it should. But why?
The way I see it: It starts with n=6 and concludes that 6 is not 1 or 2. So it calls FoneFive(n-1) + FoneFive(n-2). (This is probably where I get it wrong. But the way I see it is that this return nothing unless n is 1 or 2. So from my point of view it will narrow both down n = 1 or 2 and there by say 1 + 1 which of course is 2.)
Can someone tell me how it returns 8 ?
Calculating FoneFive(6) requires to calculate FoneFive(5) and FoneFive(4)
(as 5 and 4 are n-1 and n-2 for n=6)
Calculating FoneFive(5) requires to calculate FoneFive(4) and FoneFive(3)
(as 4 and 3 are n-1 and n-2 for n=5)
Calculating FoneFive(4) requires to calculate FoneFive(3) and FoneFive(2)
(as 3 and 2 are n-1 and n-2 for n=4)
Calculating FoneFive(3) requires to calculate FoneFive(2) and FoneFive(1)
(as 2 and 1 are n-1 and n-2 for n=3)
Both FoneFive(1) and FoneFive(2) returns 1
so FoneFive(3) = FoneFive(2) + FoneFive(1) = 1 + 1 = 2
so FoneFive(4) = FoneFive(3) + FoneFive(2) = 2 + 1 = 3
so FoneFive(5) = FoneFive(4) + FoneFive(3) = 3 + 2 = 5
so FoneFive(6) = FoneFive(5) + FoneFive(4) = 5 + 3 = 8
Okay so i get it now. It so to speak splits it selv up into two pieces every time n is not 1 or 2 and then again splits itself of to two pieces if that isn't 1 or 2 either.
f6 = f5 + f4
f5 + f4 = f4 + f3 + f3 + (f2=1)
f4 + f3 + f3 + (f2=1) = f3 + (f2=1) + (f2=1) + (f1=1) + (f2=1) + (f1=1) + 1
f3 + 1 + 1 + 1 + 1 + 1 + 1 = (f2=1) + (f1=1) + 1 + 1 + 1 + 1 + 1 + 1
(f2=1) + (f1=1) + 1 + 1 + 1 + 1 + 1 + 1 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8
I have two regression models without random effects: one is OLS using lm, the other includes multiplication of coefficients using nle.
I wish to add individual-level random effects to both. I've managed to do this for the OLS function using the lme4 package, but haven't been able to find a way to do it for the multiplicative model.
The following code produces a dataset with similar structure to the one I am working on:
df <- data.frame(id = rep(1:1000, each=10), jit = rep(rnorm(1000, 0, 0.2), each = 10), a = sample(1:5, 10000, T), b = sample(1:5, 10000,T), c = sample(1:5, 10000, T))
df <- cbind(df, model.matrix(~ as.factor(a) + as.factor(b) + as.factor(c), data.frame(rbind(as.matrix(df), t(matrix(rep(1:5, each = 5), nrow=5)))))[1:nrow(df),2:13])
colnames(df)[6:17] <- (dim_dummies <- as.vector(outer(2:5, letters[1:3], function(x, y) paste(y, x, sep=""))))
true_vals <- list(vL2 = 0.4, vL3 = 0.5, vL4 = 0.8, vA = 0.7, vB = 1.1, vC = 0.9)
attach(df)
attach(true_vals)
df$val <-
(a2 * vA + b2*vB + c2*vC) * vL2 +
(a3 * vA + b3*vB + c3*vC) * vL3 +
(a4 * vA + b4*vB + c4*vC) * vL4 +
(a5 * vA + b5*vB + c5*vC) + runif(1, -.2, .2) + jit
detach(true_vals)
detach(df)
df[1:15, ]
id jit a b c a2 a3 a4 a5 b2 b3 b4 b5 c2 c3 c4 c5 val
1 1 -0.14295 4 4 1 0 0 1 0 0 0 1 0 0 0 0 0 1.1698
2 1 -0.14295 5 1 4 0 0 0 1 0 0 0 0 0 0 1 0 1.1498
3 1 -0.14295 5 4 4 0 0 0 1 0 0 1 0 0 0 1 0 2.0298
4 1 -0.14295 5 1 5 0 0 0 1 0 0 0 0 0 0 0 1 1.3298
5 1 -0.14295 5 4 2 0 0 0 1 0 0 1 0 1 0 0 0 1.6698
6 1 -0.14295 1 5 1 0 0 0 0 0 0 0 1 0 0 0 0 0.8298
7 1 -0.14295 3 2 5 0 1 0 0 1 0 0 0 0 0 0 1 1.4198
8 1 -0.14295 3 2 1 0 1 0 0 1 0 0 0 0 0 0 0 0.5198
9 1 -0.14295 3 2 4 0 1 0 0 1 0 0 0 0 0 1 0 1.2398
10 1 -0.14295 5 3 3 0 0 0 1 0 1 0 0 0 1 0 0 1.4298
11 2 -0.01851 4 5 3 0 0 1 0 0 0 0 1 0 1 0 0 1.9643
12 2 -0.01851 2 1 3 1 0 0 0 0 0 0 0 0 1 0 0 0.5843
13 2 -0.01851 2 1 3 1 0 0 0 0 0 0 0 0 1 0 0 0.5843
14 2 -0.01851 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 -0.1457
15 2 -0.01851 2 3 1 1 0 0 0 0 1 0 0 0 0 0 0 0.6843
...
a, b, and c represent scores on three 1:5 dimension scales. a2 through c5 are dummy variables representing levels 2:5 on the same scales. There are 10 observations per individual (id). val is a proxy for the score I wish to predict using the regression models. (The values in the actual data may not correspond to the structure here, however.)
I have two regression models without random effects. One is a regular OLS using the 12 dummy variables as predictors of val:
additive.formula <- as.formula("val ~
a2 + a3 + a4 + a5 +
b2 + b3 + b4 + b5 +
c2 + c3 + c4 + c5")
fit.additive <- lm(additive.formula, data = df)
The second assumes that the relative distance between the levels is shared for the three dinensions (a,b,c), but that the dimensions differ in terms of scale. That leaves 6 coefficients (cA, cB, cC, cL2, cL3, cL4) + the intercept.
multiplicative.formula <- as.formula(" val ~ intercept +
(a2 * cA + b2*cB + c2*cC) * cL2 +
(a3 * cA + b3*cB + c3*cC) * cL3 +
(a4 * cA + b4*cB + c4*cC) * cL4 +
(a5 * cA + b5*cB + c5*cC)")
multiplicative.start <- list(intercept = 0, cA = 1, cB = 1, cC = 1, cL2 = 1, cL3 = 1, cL4 = 1)
fit.multiplicative <- nls(multiplicative.formula, start=multiplicative.start, data=df, control = list(maxiter = 5000))
Since there are 10 observations per individual, we cannot expect them to be fully independent. Therefore, I wish to add a random effect at the level of individual as defined by the variable id. I've found a way to do that with the lme4 package:
require(lme4)
additive.formula.re <- as.formula("val ~ (1 | id) +
a2 + a3 + a4 + a5 +
b2 + b3 + b4 + b5 +
c2 + c3 + c4 + c5")
fit.additive.re <- lmer(additive.formula.re, data=df)
The question is if it is possible to add random effects on the id variable using a regression model similar to the multiplicative one, maybe with the lme4 or nlme packages? The formula should look something like
multiplicative.formula.re <- as.formula(" val ~ (1 | id) + intercept +
(a2 * cA + b2*cB + c2*cC) * cL2 +
(a3 * cA + b3*cB + c3*cC) * cL3 +
(a4 * cA + b4*cB + c4*cC) * cL4 +
(a5 * cA + b5*cB + c5*cC)")
Any suggestions?
Try nlme. This should be what you need (if I understood correctly):
library(nlme)
fit.multiplicative.nlme <- nlme( model = val ~ intercept +
(a2 * cA + b2*cB + c2*cC) * cL2 +
(a3 * cA + b3*cB + c3*cC) * cL3 +
(a4 * cA + b4*cB + c4*cC) * cL4 +
(a5 * cA + b5*cB + c5*cC),
fixed = intercept + cA +cB + cC + cL2 + cL3 + cL4 ~ 1,
random = intercept ~ 1|id,
start = unlist(multiplicative.start), data=df)
However, this didn't converge when I tried it with the non-reproducible data you provide (you should set a random seed). You could try different settings in nlmeControl.
The below was incorrect:
I don't see a reason for non-linear least squares. Let's revert the dummy encoding:
df$id1 <- seq_len(nrow(df))
df$a1 <- as.integer(rowSums(df[, paste0("a", 2:5)]) == 0)
df$b1 <- as.integer(rowSums(df[, paste0("b", 2:5)]) == 0)
df$c1 <- as.integer(rowSums(df[, paste0("c", 2:5)]) == 0)
library(reshape2)
DFm <- melt(df, id.vars = c("id", "jit", "a", "b", "c", "val", "id1"))
DFm <- DFm[DFm$value == 1,]
DFm$g <- paste0("fac", substr(DFm$variable, 1, 1))
DF <- dcast(DFm, ... ~ g, value.var = "variable")
fit1 <- lm(val ~ faca + facb + facc, data = DF)
#compare results:
coef(fit.multiplicative)
prod(coef(fit.multiplicative)[c("cA", "cL2")])
coef(fit1)["facaa2"]
prod(coef(fit.multiplicative)[c("cA", "cL3")])
coef(fit1)["facaa3"]
As you see, this is basically the same model (differences are due to numerical optimization within nls). And it's easy to add a random intercept to this.
I have a greyscale image. I want to filter it such that white -> color1 and black -> color2. Both colors are in hex CSS syntax. What math do I need to do to get this effect?
I'm using this syntax:
<!-- just an example -->
<filter id="colorMeMatrix">
<feColorMatrix in="SourceGraphic"
type="matrix"
values="0 0 0 0 0
1 1 1 1 0
0 0 0 0 0
0 0 0 1 0" />
To understand the matrix you need, you have to clearly define what you're starting with, what you want to end up with, and how the colour matrix filter works.
The matrix has five columns and four rows. Each row represents one of the output numbers: R,G,B,A. The columns represent your input RGBA and a constant 1. You calculate the output value for each row by adding up each of the values in the row multiplied by the corresponding input value.
Both the input and the output numbers are standardized to the range 0-1, so you don't have to worry about multiplying everything by 256.
So for the matrix in your example:
/*R G B A 1 */
0 0 0 0 0 // R = 0*R + 0*G + 0*B + 0*A + 0
1 1 1 1 0 // G = 1*R + 1*G + 1*B + 1*A + 0
0 0 0 0 0 // B = 0*R + 0*G + 0*B + 0*A + 0
0 0 0 1 0 // A = 0*R + 0*G + 0*B + 1*A + 0
It takes the input color, adds up all four channels (RGBA) and makes the result the green channel. The red and blue channels are zero and the alpha channel isn't changed. So your picture ends up with black areas still black, but all coloured/gray/white/transparent areas are converted into shades of green.
That's not what you wanted, of course. You wanted to have black areas one colour and white areas another colour, and all gray areas somewhere in between the two.
To make black areas a certain colour, you have to set the constant-factor parameters of your matrix. The input RGB values are going to be zero for black, so they don't factor in at this point.
If your colour2, the value you want to use for black in your monochrome image, is (r2, g2, b2), then that's what your constant factors have to be:
/*R G B A 1 */
? ? ? 0 r2 // R = ?*0 + ?*0 + ?*0 + 0*0 + r2 = r2
? ? ? 0 g2 // G = ?*0 + ?*0 + ?*0 + 0*0 + g2 = g2
? ? ? 0 b2 // B = ?*0 + ?*0 + ?*0 + 0*0 + b2 = b2
0 0 0 1 0 // A = 1*A
Of course, the above matrix will turn any input colour into that output colour, because it doesn't factor in anything from the input RGBA values. To get the gradient you want, you need white areas -- those with input values of 1 for R, G, and B, to end up with your colour1, which I'm going to write as (r1, g1, b1).
Now, to make things a little easier, remember that for a grayscale image the R, G and B values for any point will be equal. So we can just use any one of those values as the input and ignore the others. So if we just set the R-factor for each channel, when the input value is white the input R equals 1 and the equations are
/*R G B A 1 */
? 0 0 0 r2 // R = ?*1 + r2 = r1
? 0 0 0 g2 // G = ?*1 + g2 = g1
? 0 0 0 b2 // B = ?*1 + b2 = b1
0 0 0 1 0 // A = 1*A
Simple algebra tells you that in order to make those equations work, you need to replace the question marks with the difference between the colour1 and colour2 values:
/* R G B A 1 */
(r1-r2) 0 0 0 r2 // R = (r1-r2)*1 + r2 = r1
(g1-g2) 0 0 0 g2 // G = (g1-g2)*1 + g2 = g1
(b1-b2) 0 0 0 b2 // B = (b1-b2)*1 + b2 = b1
0 0 0 1 0 // A = 1*A
For example, if you want white areas of the input image to map to cyan (r=0,g=1,b=1) and your black input areas to map to a deep purple (r=0.1, g=0, b=0.2), you would use a matrix like
/*R G B A 1 */
-0.1 0 0 0 0.1 // R = (-0.1)*R + 0.1
1 0 0 0 0 // G = 1*R + 0
0.8 0 0 0 0.2 // B = 0.8*R + 0.2
0 0 0 1 0 // A = 1*A
Using that matrix in a filter applied to this original image.
Note that this is actually quite different from the example I linked to in a comment; in that filter, I was trying to maintain white as white and black as black, but change the grays to colour. For that I used a gamma correction filter, not a colour matrix.