prove the following identity holds for all real numbers x. [4x]=[x]+[x+1/4]+[x+2/4]+[x+3/4] - discrete-mathematics

The brackets are the floor function. so [4.3] would be 4 and [-2.4] would be -3 I am trying to prove this by cases. I think that I should prove each case first such as [x] first and then [x+1/4] and on but I'm having trouble proving it in generality

Prove it in four cases.
Case 1: 0 <= x - [x] < 1/4
Case 2: 1/4 <= x - [x] < 1/2
Case 3: 1/2 <= x - [x] < 3/4
Case 4: 3/4 <= x - [x] < 1
In each case, [x], [x+1/4], [x+2/4], [x+3/4], and [4x] are easy to express in terms of [x].

Related

Matlab "diff" in F# (subtract element by element)

There is a diff function in MATLAB, that computes difference between elements in vector (or matrix, but it is not the case now).
X = [1 1 2 3 5 8 13 21];
Y = diff(X)
Results in: 0 1 1 2 3 5 8
I came up with F# solution:
let x =[1;1;2;3;5;8;13;21]
let diff x = List.map2 (-) (x|> List.tail) (x|> List.take ((x|>List.length) - 1))
diff x
which results in same List, but I feel there should be a better way how to do difference in F#? Is there?
There's List.pairwise : 'T list -> ('T * 'T) list which gives you a list of consecutive pairs of items.
let x =[1;1;2;3;5;8;13;21]
let diff x =
x |> List.pairwise |> List.map (fun (x, y) -> y - x)
Working with sequences instead of lists there is a compact solution:
let diff x = Seq.map2 (-) (Seq.skip 1 x) x
This would not work with lists because List.map2 requires its arguments to have the same length. Seq.map2 does not have that requirement.
For your specific case you could do:
[1;1;2;3;5;8;13;21] |> diff |> List.ofSeq
if you want the result to be a list.

How to add a constraint to my optimization?

I am working on formulating an optimization problem where I have a 2-D matrix A.
A= [0 f1 0 f2]
[f3 f3 0 0]
.........
And I have another 2-D matrix B that I should fill. B has the same size of A. I need b_ij (element of B) to be zero if a_ij=0 (element of A) and I need b_ij to be greater than zero and less than or equal to a_ij if a_ij is not zero.
How can I represent this in my formulation? I have added this constraint/condition:
b_ij<=a_ij
But this does not satisfy the condition that states that b_ij is not equal zero when a_ij is not equal zero. Any help?
If all elements are positive, keep the smallest element of each matrix by doing an element by element comparison :
B2 = min(A,B)
Alternatively, create a logical matrix indicating if a condition is answered and multiply element by element with the matrix B , only elements who satisfy the condition remain, others are set to zero:
B = B.*(A~=0)
Then keep elements of B that are smaller or equal to elements of A, and replace them by the value of A otherwise.
B = B.*(B<=A) + A.*(B>A) )
This option lets you generalize your constraint.
You indicate needing elements of b_ij to be greater than zero if elements of a_ij are greater than zero. An option is to use the function max to ensure that all elements of B are positive.
B = max(1e-2,B); % exact value is yours to set.
This step is up to you and depend on your problem.
You want to implement the implication
a = 0 => b = 0
a <> 0 => 0 < b <= a
If a is (constant) data this is trivial. If a is a variable then things are not so easy.
You implemented part of the implications as
b <= a
This implies a is non-negative: a>=0. It also implies b is non-negative. The remaining implication a>0 => b>0 can now be implemented as
a <= δ * 1000
b >= δ / 1000
δ in {0,1}
Many MIP solvers support indicator constraints. That would allow you to say:
δ = 0 -> a = 0
δ = 1 -> b >= 0.001
δ in {0,1}

Best way to do plus or minus

Is there a native or simpler way to do plus or minus when comparing two double values. Right now I have done two comparisons like this but I feel like there should be a cleaner way to do this. We are doing a lot of these comparisons in our app so I would prefer to use the simplest and cleanest function for this.
let y: Double = 5 // main value
let x: Double = 6 // value that we are comparing y to
if x > y + 2 || x > y - 2 {
//Do something
}
I am basically looking to check if x (in this case 6) is between 3 (5-2) and 7 (5+2). I want to do something if it is between the two value and do something else if it is not.
If possible, I want to do it by using the value of 2 opposed to explicitly writing out 3 and 7.
To check whether x is within 2 of y:
if abs(x - y) < 2 {
...
}
It's simple enough to understand alone, I think, but for the sake of completeness:
this moves the test to being around 0. If x is exactly equal to y then x - y is 0. If x is two less than y then x - y is -2. If it is two greater than x - y is 2. If it is anywhere between then it is somewhere in the middle. So the interesting range is (-2, 2);
applying an abs both gives an interesting range of [0, 2) and guarantees no outcome is below 0;
therefore a comparison only to the upper end of the range is sufficient.
You can use pattern matching:
if (y-2)...(y+2) ~= x {
// your code in case the value is within the range
}
Depending on your use it may be helpful to check to see if X is in a set of numbers.

Perceptron - MatLab Serious Confusion

This is my first stab at machine learning, and I can implement the code anyway that I want. I have Matlab access, which I think will be simpler than Python, and I have pseudo code for implementing a PLA. The last part of the code, however, absolutely baffles me, though it is simpler than the code I have seen on here thus far. It seems to be calling for the use of variables not declared. Here's what I have. I'll point out the number line at which I get stuck.
1) w <- (n + 1) X m (matrix of small random nums)
2) I <- I augmented with col. of 1s
3) for 1 = 1 to 1000
4) delta_W = (N + 1) X m (matrix of zeros) // weight changes
5) for each pattern 1 <= j <= p
6) Oj = (Ij * w) > 0 // j's are subscript/vector matrix product w/ threshold
7) Dj = = Tj - Oj // diff. between target and actual
8) w = w + Ij(transpose)*Dj // the learning rule
Lines 1 thru 4 are coded.
My questions are on line 5: What does "for each pattern mean" (i.e., how does one say it in code). Also, which j are they interested in, I have a j in the observation matrix and a j in the target matrix. Also, where did "p" come from (I have i's, j's, m's and n's but no p's)? Any thoughts would be appreciated.
"for each pattern" refers to the inputs. All they are saying is to run that loop where Ij is the input to the perceptron.
To write this in MATLAB, it really depends on how your data is oriented. I would store your inputs as a mXn matrix, where m is the number of inputs and n is the size of each input.
Say our inputs look like :
input = [1 5 -1;
2 3 2;
4 5 6;
... ]
First 'augment' this with a column of ones for the bias input:
[r c] = size(input);
input = [input ones(r,1)];
Then, your for loop will simply be:
for inputNumber = 1:r
pattern = input(inputNumber,:);
and you can continue from there.

Discrete mathematics statement

Is this statement true ?
∀x ∈ R, ∃y ∈ R,(x ≥ y) ⇒ (x > y)
I believe it is not because for example if x is 5 and y is 5 it satisfies "(x ≥ y)" but it doesn't mean that it is also "(x > y)".
Am I correct ? Your input would be much appreciated.
Yes, the statement is true.
Given x in R (which I presume stands for the real numbers), let y = x - 1. Then, we need to check whether (x >= y) => (x > y) is true. Both the left and right sides of the implication are true (although only the right one needs to be), so we have true => true, which evaluates to true as well.
So, for any x you give me, I've just given you a y that makes the desired implication hold.
While the statement is true as written it doesn't mean what you probably think it does.
Edit: Interestingly, if I say, given x, let y = x + 1, then the implication statement also evaluates to true. This is because then x >= y fails, and so does x > y, so we have false => false, which also evaluates to true.
For more information, see:
http://en.wikipedia.org/wiki/Vacuous_truth
http://en.wikipedia.org/wiki/Truth_table