Use param3d to plot a curve C1(u)=Au2+Bu+C, which passes through
(0,0,0) at u=0,
(1,0,0)at u=1,
(1/2,1/2, 0) at u=0.5.
Have to use param3d .
Just write down the equations, using matrix block form you have
| 0 0 1 | | A | | 0 0 0 |
| 1 1 1 | * | B | = | 1 0 0 |
| 1/4 1/2 1 | | C | | 1/2 1/2 0 |
hence you just have to solve this equation for the [A;B;C] matrix then extract A,B,C and plot the curve
ABC = [0 0 1;1 1 1;1/4 1/2 1] \ [0 0 0;1 0 0;1/2 1/2 0];
A = ABC(1,:);
B = ABC(2,:);
C = ABC(3,:);
u = linspace(0,1,100);
C1 = A'*u.^2+B'*u+C'*ones(u);
param3d(C1(1,:),C1(2,:),C1(3,:));
It is easy to see in advance that C=(0,0,0) here, but the above method is general.
Related
I hope you can help me.
I would like to know how to read an output in R.
So I have three binary variables Q3A1, Q3A2, Q3A3, each line corresponds to the answer of a subject
Q3A1
Q3A2
Q3A3
0
1
0
1
0
1
0
1
0
1
0
1
I wanted to have the number of people for each possible answer, so I wrote the command
table(dataQ$Q3A1,dataQ$Q3A2,dataQ$Q3A3)
and I got this as an output in R console
, , = 0
| | 0 |1 |
| 0 | 90 |16|
| 1 | 80 |61|
, , = 1
| | 0 |1 |
| 0 | 19 |4 |
| 1 | 52 |13|
but I don't know how to read it, which rows and columns corresponds to which variable ?
Thanks in advance for your help !
(AVB)&(AV~B) is logically equivalent to ~~A?
It kind of confused me since they are in different dimension.
Starting from:
(A∨B)∧(A∨¬B)
One can first apply one of De Morgan's laws to the AND:
¬(¬(A∨B)∨¬(A∨¬B))
Next, one of De Morgan's laws can be applied to each side:
¬((¬A∧¬B)∨(¬A∧B))
By applying distributivity of AND over OR in reverse, ¬A can be extracted:
¬(¬A∧(¬B∨B))
By complementation, ¬B∨B is 1:
¬(¬A∧1)
By identity for AND:
¬¬A
Applying double negation:
A
This goes through your target ¬¬A without going through simple A. It's much simpler not to do that.
Starting again from:
(A∨B)∧(A∨¬B)
By applying distributivity of OR over AND in reverse, A can be extracted:
A∨(B∧¬B)
By complementation, B∨¬B is 1:
A∧1
By identity for AND:
A
Applying double negation in reverse:
¬¬A
Since you mention doing it by truth table, here's my go at showing that they're equivalent that way:
+---+---+----+----+-------+--------+--------------+-----+
| A | B | ¬A | ¬B | (A∨B) | (A∨¬B) | (A∨B)∧(A∨¬B) | ¬¬A |
+---+---+----+----+-------+--------+--------------+-----+
| 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
+---+---+----+----+-------+--------+--------------+-----+
If got data looking like this:
A | B | C
--------------
f | 1 | 1420h
f | 1 | 1540h
f | 3 | 600h
g | 2 | 900h
g | 2 | 930h
h | 1 | 700h
h | 3 | 400h
Now I want to create a new column which counts other rows in the data frame that meet certain conditions.
In this case I would like to know in each row how often the same combination of A and B occured in a range of 100 around C.
So the result with this data would be:
A | B | C | D
------------------
f | 1 | 1420 | 0
f | 1 | 1540 | 0
f | 3 | 1321 | 0
g | 2 | 900 | 1
g | 2 | 930 | 1
h | 1 | 700 | 0
h | 3 | 400 | 0
I actually came to a solution using for(for()). But the time R needs to compute the resuts is tooooo long.
for(i in 1:nrow(df)) {
df[i,D] <- sum( for(p in 1:nrow(df)) {
df[p,A] == df[i,A] &
df[p,B] == df[i,B] &
df[i,C] +100 > df[p,C] &
df[p,C] > df[i,C]-100 } ) }
Is there a better way?
Thanks a lot!
I want to take a set of observations & find out how much overlap different columns have based on the indicators. I have the following data:
uniquevalue | X | Y | Z |
Obs 1 | 1 | 0 | 1 |
Obs 2 | 1 | 1 | 0 |
Obs 3 | 1 | 0 | 1 |
Obs 4 | 0 | 1 | 0 |
Obs 5 | 0 | 0 | 1 |
Obs 6 | 0 | 1 | 0 |
Obs 7 | 0 | 0 | 1 |
I want to create the following data overlap matrix:
Label | X | Y | Z |
X | 100% | 33% | 50% |
Y | 33% | 100% | 0% |
Z | 66% | 0% | 100% |
So, for example, Z has a total of 4 observations. 2 of its 4 observations are also present on X, so its overlap % is 50%. However because different columns have different numbers of observations, the reverse is not necessarily true. As you can see, 2 of 3 observations in X are shared with Z, so its a 66% overlap.
You can use crossprod:
mat <- crossprod(as.matrix(df[2:4])) # calculate the overlap
floor(t(mat * 100 / diag(mat))) # calculate the percentage
# X Y Z
#X 100 33 50
#Y 33 100 0
#Z 66 0 100
I'm writing a multiplication program for a virtual computer called AIDJ.
It supports any number of variables and is able to read and execute any sequence of operations (programme) of the form
mk: <operation>
Where mk is the marker and must only be taken from:
Increment: x(i) = x(i) + 1
Decrement: x(i) = x(i) - 1
Conditional Jump: if x!=0 goto <mk>
Assignment: x(i) = c (where c is any natural number)
Apart from potential conditional jumps - AIDJ evaluates the operations in the order described by the programme and stops when the programme is completely processed.
Take for example the programme ADD with c >= 1:
00: x(1) = c
01: x(2) = d
02: x(2) = x(2) + 1
03: x(1) = x(1) - 1
04: if x(1) != 0 goto 02
Take for example the programme MULTIPLY with c >= 2:
00: x(1) = d
01: x(1) = d - 1
02: x(2) = c
03: x(3) = c
04: x(2) = x(2) + 1
05: x(3) = x(3) - 1
06: if x(3) =! 0 goto 04
07: x(1) = d - 1
08: if x(3) =! 0 goto 03
I am making sure that c × d equals c + c + … + c (d summands) so I can use the programme ADD, and we make sure that ADD is executed d-1 times. When the computation stops, x(2) equals c × d.
This would produce following table of values if c=3 and d=3:
| Command | x(1) | x(2) | x(3) |
--------------------------------------------------
| 00: x(1) = d | 3 | - | - |
| 01: x(1) = d - 1 | 2 | - | - |
| 02: x(2) = c | 2 | 3 | - |
| 03: x(3) = c | 2 | 3 | 3 |
| 04: x(2) = c + 1 | 2 | 4 | 3 |
| 05: x(3) = d - 1 | 2 | 4 | 2 |
| 06: if x(3) =! 0 goto 04 | 2 | 4 | 2 |
| 04: x(2) = c + 1 | 2 | 5 | 2 |
| 05: x(3) = d - 1 | 2 | 5 | 1 |
| 06: if x(3) =! 0 goto 04 | 2 | 5 | 1 |
| 04: x(2) = c + 1 | 2 | 6 | 1 |
| 05: x(3) = d - 1 | 2 | 6 | 0 |
| 06: if x(3) =! 0 goto 04 | 2 | 6 | 0 |
| 07: x(1) = d - 1 | 1 | 6 | 0 |
| 08: if x(3) =! 0 goto 03 | 1 | 6 | 0 |
| 03: x(3) = c | 1 | 6 | 3 |
| 04: x(2) = c + 1 | 1 | 7 | 3 |
| 05: x(3) = d - 1 | 1 | 7 | 2 |
| 06: if x(3) =! 0 goto 04 | 1 | 7 | 2 |
| 04: x(2) = c + 1 | 1 | 8 | 2 |
| 05: x(3) = d - 1 | 1 | 9 | 1 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 1 |
| 04: x(2) = c + 1 | 1 | 9 | 1 |
| 05: x(3) = d - 1 | 1 | 9 | 0 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 0 |
| 07: x(1) = d - 1 | 0 | 9 | 0 |
| 08: if x(3) =! 0 goto 03 | 0 | 9 | 0 |
How would I alter the the above program so that it divides instead of multiply?
Multyplying is adding in a loop, right? Well, dividing is subtracting in a loop until what's left of the dividend is less than the divisor. In pseudocode:
# We're dividing a/b, getting the quotient c
c = 0
while a >= b
{
a = a - b
c = c+1
}
# End of loop
# The value of a is the remainder at this point
So go with this. First, implement subtraction (which is decrement in a loop, on your VM). Then do the rest.
Since your conditionals are limited to comparing with zero, implement the logic of a>=b via subtraction (decrement in a loop). And don't be afraid to introduce extra variables. The architecture might be stunted, but there's no explicit limit on memory used, is there?
You can even combine the loop condition (which is decrement in a loop) with the loop body (also decrement in a loop). That's not how they'd do it in a high level language, and not even how a real CPU would do it - all modern CPUs have integer comparison as a primitive. Something along the lines of "decrement, if reached zero before the remain of the divisor reaches zero, then quit the loop and we're done". In real life, however, this kind of microoptimization (for a nonexistent architecture, no less) would be strongly recommended against.
Also, if this is homework, please add the tag "homework". Smells a lot like homework to me.
EDIT: here's the logic. We introduce three variables:
x(1) is the running copy of the dividend to be decremented
x(2) is the quotient - zero initially, to be incremented
x(3) is the running loop variable for the divisor.
d is the dividend
c is the divisor.
we can assign constants (like zero) to variables, can we?
Using a pseudocode that corresponds to the primitives of your VM. You translate it to AIDJ syntax - I mean, you have to do at least some work here. # marks comments. Labels (i. e. goto destinations) are marked with :
x(1) = d
x(2) = 0
x(3) = c
Main_Loop:
x(1) = x(1) - 1 # decrement the running copy of the dividend
x(3) = x(3) - 1 # decrement the running copy of the divisor
if x(3) != 0 goto Divisor_still_nonzero
# If we're here, this means we've reached the end of the divisor.
# Increment the quotient, reset the running divisor to the initial value
x(2) = x(2) + 1
x(3) = c
Divisor_still_nonzero:
# Now let's check if we've reached the zero on the dividend
if x(1) != 0 then goto Main_Loop
# If x(1) is zero then we end up here.
# Means we've exhausted the dividend.
# Now the value of x(2) is the quotient.
# The remainder is c minus x(3),
# but retrieving it was not a requirement.
In real life, you should also check if c and d are zero or negative in the very beginning. If d is zero, the code will break - it'll go into negatives on the very first iteration, and will terminate only if the variables are subject to overflow. If c is zero, the whole division is illegal.
Extra handling for negatives is required, too.
Also, keep in mind that some professors are known to watch Stack Overflow.