Hi, what does the following line in Julia do? Thank you [closed] - julia

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
H = H === nothing ? h : [H h]
#both matrices

The matrix H is being assigned a value based on a ternary operator, which takes the form a ? b : c. In this case, if H is nothing, it will made the same as h. Otherwise, it will be concatenated with h (as with hcat).
Note: When H === nothing, the code does not assign H the values of h, as has been suggested. Instead, H is a copy of h. For instance, if you change one of the elements in H, it will change an element in h, since they are the same matrix.
It may help to look at the documentation for arrays.

Suppose
H === nothing
evaluates to true, then H is assigned the value of h. If false, then H is assigned to a 1x2 matrix which has H as its first value and h as its second value.

Related

Bidirectional assignment operator in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there an R function that assigns variables bidirectionally? For example, let <-> represent a bidirectional assignment operator.
a <-> b
a
> b
b
> a
One can define something like:
`%<->%` <- function(x,y){
t <- y
assign(deparse(substitute(y)), x, envir=parent.frame())
assign(deparse(substitute(x)), t, envir=parent.frame())
}
a <- 1
b <- 2
a %<->% b
a
[1] 2
b
[1] 1
Such an operator would not make sense with the way R functions:
From Hadley Wickham's book Advanced R, section "Binding basics":
Consider this code:
x <- c(1, 2, 3)
[...] this code is doing two things:
It’s creating an object, a vector of values, c(1, 2, 3).
And it’s binding that object to a name, x.
So, for instance, when you run:
a <- 1
you are creating a numerical vector with one element and you are binding it to the name a.
a <-> b
would be binding names to one another, which makes no sense in R.
Also note than when you do:
a <- 1
b <- a
b
# [1] 1
You get 1 as the output, not a, because you create another binding (b) to the numerical vector with the value 1. And when you run b, the output is the object binding to it (1), not another name this object is binding to.
Note: Hadley explains all this very clearly with diagrams in his book.

Formula to find square root of a natural number using only addition and subtraction [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
How to use only addition and subtraction to find out square root of a natural number?
Thanks.
[I have looked over the internet but i didn't find any content related to this problem.]
Explanation to my problem: I want to create a c function which will receive only a natural number and return square root of it.
You may say to use the "sqrt" function but i just thought of creating one which will utilize addition and subtraction operator to create the square root.
You don't have to write the program, just writing the formula for it will be just fine. Thanks.
Update: This question is not specifically about coding rather about mathematics.(I tagged "c" as it had some link but this question is NOT about coding.)
n2 is the equivalent of the sum of the n first odd numbers.
You can iterate over the n (consequently adding only the next odd number to the previously calculated sum) until the square is equal or exceeds your number.
k = 0
sum = 0
while sum < target:
k += 1
sum += 2k-1
if sum > target:
the target doesn't have integer root
else:
k is the square root
We can use the fact that (n+1)² = n² + 2n + 1.
def sqrt(n):
k = 0
s = 0
while s <= n:
s = s+k+k+1
k = k+1
return k-1
print (sqrt(0)) # 0^2
print (sqrt(1)) # 1^2
print (sqrt(2)) # (1.4142135623730951...)^2
print (sqrt(144)) # 12^2
print (sqrt(169)) # 13^2
print (sqrt(196)) # 14^2
print (sqrt(255)) # 15^2
print (sqrt(1000)) # (31.622776601683793...)^2
print (sqrt(2000)) # (44.721359549995796...)^2
UPD
Sorry, I thought you were asking for code :)

AB+C when you only have ABC and C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I'm trying to calculate AB+C (A, B and C being boolean values) but I only have the values ABC and C. I can negate, OR, and AND to my heart's content, but I am only getting ABC and C out of the system.
Specifically there is a service that has two boolean state variables. One is a combination of three internal properties and is only true when all three are true, but one of the three state properties is also available. I'm just trying to figure out if I can calculate the value I want without asking for them to add A and B as separate values.
AB is A and B
A + B is A or B
I don't think you can compute AB+C in any case knowing only the value of C and ABC:
If C is true, AB+C is true
If C is false, ABC is false and you can't find out anything about AB.
Old Answer - which is wrong as stated in the comments:
I'll give it a try. You can construct not(not(ABC)+C)+C:
not(not(ABC)+C)+C = not(not(A)+not(B)+not(C)+C))+C
= not(not(A)+not(B))+C
= AB+C

Write logic gates from logic gate circuit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Could someone please show me how to write the logic from this logic circuit?
Following you guys' explanation, I have these results, am I right:
The question is which one of a, b, c, d produces the same ouput as the first circuit. I don't see any similar results here, please illuminate me !
Start from the inputs:
The AND gate gets A and B as inputs, and the NOT gate gets B as its input. The outputs of these 2 are the inputs to the OR Gate.
Therefore let
Y = A AND B
and Z = (NOT B)
=> X = Y OR Z
=> X = (A AND B) OR (NOT B)
UPDATE
I think b is the right answer.
If you give names to the intermediate terms, e.g. S and T, where S is the output of the AND gate and T is the output of the inverter (NOT gate), then you can break it down as follows:
X = S | T ; final OR gate
S = A & B ; output of AND gate
T = ~B ; output of inverter (NOT gate)
X = (A & B) | ~B ; substitute above
Note that this is a poor example as the expression can be reduced to:
X = ~(~A & B)
which can be implemented with just two gates (a NAND and a NOT), or
X = A | ~B
which can also be implemented with two gates (an OR and a NOT).

What is the meaning of ∃? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Reading a book on algorithms. Can someone explain the meaning of the mathematical symbol ∃?
It is called a quantifier. It means "there exists".
When used in an expression such as
∃x s.t. x > 0
It means "There exists a number x such that x is greater than 0."
Its counterpart is ∀, which means "for all". It's used like this:
∀x, x > 0
Which means "For any number x, it is greater than 0."
It is the "existential quantifier" as opposed to the upside-down A (∀) which means "universal quantifier." It should be read as "there exists" or "for some". It is a predication that means that some relation or property holds true for at least one object in the domain.
Examples:
An integer n is composite if ∃ integer m such that m > 1 and m < n with n divisible by m.
An integer n is prime if ∀ integer m such that m > 1 and m < n it is true that n is not divisible by m.
A function f is continuous on a metric space (X, d) if ∀x∀ε>0∃δ>0 | ∀y d(x, y) < δ => d(f(x), f(y)) < ε
More Info on Predicate Logic
It is called existential quantifier and being followed by x, it means there exists at least one x
For future reference, wikipedia has a table of mathematical symbols, with an explanation of the meaning(s) of each one.

Resources