Gradient of high dimensional function I hope this is the right forum for this.
Here are the givens: 
$X$ is a matrix $1230 x 30$ with the following properties:
In a line, all values are $0$ except one $1$ and one $-1$. 
$Y = \{0,1\}^{1230}$
$B = (b_1,...,b_{30})^{-1}$
The Function works like this: 
$$
f_1 = -\sum_{i=1}^{N}(Y_i*log(x_iB)+(1-Y_i)*log(-1*(x_iB)) 
$$
The Properties of X mean, I think, that it can also be written like this:
$$
f_2 = -\sum_{i=1}^{N}(Y_i*log(b_k-b_l)+(1-Y_i)*log(b_l-b_k) 
$$
Now I want to calculate the Gradient of tat function.
I calculated that 
$$
\frac{\delta}{\delta{b_k}}(Y_i*log(b_k-b_l)+(1-Y_i)*log(b_l-b_k) = \frac{1}{b_k-b_l} 
$$
But now I'm not hundred percent sure how to actually evaluate that. From the math I know, the correct derivative for $ f_2 $ should be:
$$
\frac{\delta{f_2}}{\delta{b_k}} = -\sum_{i=1}^{N}\frac{1}{b_k-b_l} 
$$
But the step from that to $\frac{\delta{f_1}}{\delta{b_k}}$ seems to elude me. 
The goal is to be able to evaluate the Gradient at a given $B$. My first intuition was the folowing:
$$
-\sum_{i=1}^{N}\frac{1}{x_iB}
$$
But does is not really a gradient, but rather a function. Somehow I have to be able to differentiate, for each b, if it is in a given summand or not. How can that be done.
 A: Let's denote the vectors by lowercase letters $\{y,b\}$ and the matrix by an uppercase $\{X\}$.
Then a simplified version of your problem is 
$$\eqalign{
  f &= y^T\log(Xb) = y:\log(Xb)
}$$where the colon represents the Frobenius Inner Product and log() is evaluated elementwise.  The Frobenius product commutes with the Hadamard (elementwise) product, and has other convenient properties.
One more notation that will prove handy, the Hadamard (elementwise) division of vector $a$ by the vector $b$ will be denoted as $\frac{a}{b}$.
Remembering the basic rule
$$\eqalign{d\log(z) &= \frac{dz}{z}\cr}$$
we can take the differential of the function
$$\eqalign{
 df &= y:\frac{X\,db}{Xb} \cr
  &= \frac{y}{Xb}:X\,db \cr
  &= {\rm Diag}(Xb)^{-1}\,y:X\,db \cr
  &= X^T\,{\rm Diag}(Xb)^{-1}\,y:db \cr
}$$where Diag() creates a diagonal matrix from its vector argument.
Since $df=\big(\frac{\partial f}{\partial b}:db\big),\,$ the gradient must be
$$\eqalign{
 \frac{\partial f}{\partial b} &= X^T\,{\rm Diag}(Xb)^{-1}\,y \cr
}$$
