I've written the following code*:  function val = newton_backward_difference_interpolation(x,y,z) % sample input : newton_backward_difference_interpolation([0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84 086 0.88],[0.8423 0.8771 0.9131 0.9505 0.9893 1.0296 1.0717 1.1156 1.1616 1.2097],0.71) % sample output: % X-Input must be equidistant %   -1.1102e-16 n=numel(x);% number of elements in x (which is also equal to number of elements in y) h=x(2)-x(1);% the constant difference %error=1e-5;% the computer error in calculating the differences [It was workaround where I took abs(h-h1)&gt;=error] if n&gt;=3% if total elements are more than 3, then we check if they are equidistant     for i=3:n         h1=x(i)-x(i-1);         if (h1~=h)             disp('X-Input must be equidistant');             disp(h1-h);             return;         end     end end ...   I also wrote a function for calculating inverse of a matrix** where this:  a=[3 2 -5;1 -3 2;5 -1 4];disp(a^(-1)-inverse(a));   displays this:    1.0e-16 *           0   -0.2082    0.2776          0    0.5551         0          0    0.2776         0    Why is there a small difference of 1e-16 occuring? Am I right in saying that this is because computer calculations (especially divisions are not exact)? Even if they are inexact, why does a^(-1) provide more accurate/exact solution (Sorry If this question is not programming related in your view.) Can this be avoided/removed? How?   *for newton backward difference interpolation: (whole code at ), the expected output is 0.85953 or something near that. **using Gauss-Jordan Elimination: (whole code at )
6537
I am writing a program where I need to delete duplicate points stored in a matrix. The problem is that when it comes to check whether those points are in the matrix, MATLAB can't recognize them in the matrix although they exist.  In the following code, intersections function gets the intersection points:  [points(:,1), points(:,2)] = intersections(...     obj.modifiedVGVertices(1,:), obj.modifiedVGVertices(2,:), ...     [vertex1(1) vertex2(1)], [vertex1(2) vertex2(2)]);   The result:  &gt;&gt; points points =    12.0000   15.0000    33.0000   24.0000    33.0000   24.0000  &gt;&gt; vertex1 vertex1 =     12     15  &gt;&gt; vertex2     vertex2 =     33     24   Two points (vertex1 and vertex2) should be eliminated from the result. It should be done by the below commands:  points = points((points(:,1) ~= vertex1(1)) | (points(:,2) ~= vertex1(2)), :); points = points((points(:,1) ~= vertex2(1)) | (points(:,2) ~= vertex2(2)), :);   After doing that, we have this unexpected outcome:  &gt;&gt; points points =    33.0000   24.0000   The outcome should be an empty matrix. As you can see, the first (or second?) pair of [33.0000 24.0000] has been eliminated, but not the second one.  Then I checked these two expressions:  &gt;&gt; points(1) ~= vertex2(1) ans =      0 &gt;&gt; points(2) ~= vertex2(2) ans =      1   % &lt;-- It means 24.0000 is not equal to 24.0000?   What is the problem?    More surprisingly, I made a new script that has only these commands:  points = [12.0000   15.0000           33.0000   24.0000           33.0000   24.0000];  vertex1 = [12 ;  15]; vertex2 = [33 ;  24];  points = points((points(:,1) ~= vertex1(1)) | (points(:,2) ~= vertex1(2)), :); points = points((points(:,1) ~= vertex2(1)) | (points(:,2) ~= vertex2(2)), :);   The result as expected:  &gt;&gt; points points =      Empty matrix: 0-by-2
41585
Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.   And now that we're here, how do they differ from a regular function?
90234
Can someone explain to me this error of the Desmos graphic calculator? Why ? For other values of $x$, the exact value of $f(x)$ is obtained.  Or am I wrong when I assure that $f(\sqrt2)=0$?
95790
A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fellow SO'ers could come up with a better solution.  I'm posting my solution as an answer to this question.  Example Output:  For a 3x3 matrix, the output should be:  (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1)    Furthermore, the algorithm should support non-square matrices, so for example for a 5x3 matrix, the output should be:  (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1) (2, -1) (2, 0) (2, 1) (-2, 1) (-2, 0) (-2, -1)
27
Consider the following code:   0.1 + 0.2 == 0.3  -&gt;  false   0.1 + 0.2         -&gt;  0.30000000000000004   Why do these inaccuracies happen?
24043
I'm trying to work with  in Java.   I want to implement arithmetic functions.  For this, I will first require a way to normalize the functions.  I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6.  A naive approach would have me add 2/12 and 6/12 and then reduce.  How can I achieve a common denominator with the least performance penalty?  What algorithm is best for this?    Version 8 (thanks to ):     Improvements include:         the equals() method is now consistent with the compareTo() method      final class Fraction extends Number {     private int numerator;     private int denominator;      public Fraction(int numerator, int denominator) {         if(denominator == 0) {             throw new IllegalArgumentException("denominator is zero");         }         if(denominator &lt; 0) {             numerator *= -1;             denominator *= -1;         }         this.numerator = numerator;         this.denominator = denominator;     }      public Fraction(int numerator) {         this.numerator = numerator;         this.denominator = 1;     }      public int getNumerator() {         return this.numerator;     }      public int getDenominator() {         return this.denominator;     }      public byte byteValue() {         return (byte) this.doubleValue();     }      public double doubleValue() {         return ((double) numerator)/((double) denominator);     }      public float floatValue() {         return (float) this.doubleValue();     }      public int intValue() {         return (int) this.doubleValue();     }      public long longValue() {         return (long) this.doubleValue();     }      public short shortValue() {         return (short) this.doubleValue();     }      public boolean equals(Fraction frac) {         return this.compareTo(frac) == 0;     }      public int compareTo(Fraction frac) {         long t = this.getNumerator() * frac.getDenominator();         long f = frac.getNumerator() * this.getDenominator();         int result = 0;         if(t&gt;f) {             result = 1;         }         else if(f&gt;t) {             result = -1;         }         return result;     } }     I have removed all previous versions.  My thanks to:
97381
$\omega&gt;0$, Then Compute the matrix:          $e^A$,     where $A= \begin{bmatrix} 0 &amp; \omega \\ -\omega&amp; 0 \end{bmatrix}$      I have never seen any problem like this. So Please help me solving this. Any link for theoretical reading is welcome.
33636
Suggest algorithm for the numerical calculation $ \frac{1}{\sqrt{a}} \ a &gt; 0 $ without division, use Newton's method.  My idea is:  $$ \frac{1}{\sqrt{a}} = (\sqrt{a})^{-1} = a^{-\frac{1}{2}}$$ $$ x = a^{-\frac{1}{2}} \Rightarrow f(x)=x^2 - a^{-1}$$ And next step is calulate $ f'(x)=2x $, but after substitution I got: $$ x_{n+1} = \frac{x_n^2+a^{-1}}{2x_n}$$ And know I still have divion, I guess. Maybe you've better idea on this task?
6894
How do I calculate relative error when the true value is zero?  Say I have $x_{true} = 0$ and $x_{test}$. If I define relative error as:  $\text{relative error} = \frac{x_{true}-x_{test}}{x_{true}}$  Then the relative error is always undefined. If instead I use the definition:  $\text{relative error} = \frac{x_{true}-x_{test}}{x_{test}}$  Then the relative error is always 100%. Both methods seem useless. Is there another alternative?
78030
I'm reading Chapter 9 of . As you can see in eq. (9.30) his model for SUSY is  $$\begin{align} \delta_\epsilon X &amp;=\epsilon^1\psi_1 + \epsilon^2\psi_2\\ \delta\psi_1 &amp;= \epsilon^2\partial h\\ \delta\psi_2 &amp;=-\epsilon^1\partial h.  \end{align}\tag{9.30}$$  I understand that if $\partial h\neq 0$ it is possible to see that partition function vanishes, in particular, there is a total divergence:  $$ \int \mathrm{d}\hat{X}e^{\frac{1}{2}(\partial h(\hat{X}))^2}\frac{\partial^2 h(\hat{X})}{(\partial h(\hat{X}))^2}. \tag{*} $$  The implication that a total divergence yields vanishing integral, it is a bit strange, because, since every function that admit a primitive is a total derivative. What is the meaning of that implication? Then why $(*)$ vanishes? It seems to me that, in general, $$ \int \mathrm{d}(h')\frac{e^{\frac{1}{2}(h')^2}}{(h')^2}\neq 0. $$  Then, again, when he claims that if there is a $x_0$ such that $h'(x_0)=0$ we can use again the argument of the total divergence, why doesn't he keep in account that, removing a small neighborhood of $x_0$, give us some contribution at the border of that neighborhood (two points)?
90632
I'm interested in algorithms to quickly compute the inverse factorial. I've noted that large factorials have a unique number of digits. How can I use this fact to quickly compute the factorial? Is there a formula,  n = f(n!) = #digits( (n!) )?  I'm mostly interested in the case where we know our input is correct. But, error checking for values that are not factorials would be a bonus. (Perhaps, someone has thought of a way to do the inverse gamma function quickly?)  I'm interested in inputs that have over a million digits, so simply dividing 1,2,3,...,n will not work.
24030
Show that Newton's method can be used to compute the square root function $\sqrt a$ using the formula   $$x_{n+1} = \frac{1}{2}\left(x_{n} + \frac{a}{x_{n}}\right)$$  show that the error is  $$\sqrt a - x_{n+1} = -\frac{1}{2x_{n}}\left(\sqrt a - x_{n}\right)^2$$  edit: As pointed out below $x^2-a$ has $\sqrt a$ as a root.  I have done as suggested below and plugged in $\sqrt a + \epsilon$ for $x_n$ giving me  $$x_{n+1} = \frac{2\epsilon\sqrt a + \epsilon^2}{2(\sqrt a + \epsilon)}$$ and once again not sure where to proceed.
62559
I'm just getting started with some machine learning, and until now I have been dealing with linear regression over one variable.  I have learnt that there is a hypothesis, which is:  $h_\theta(x)=\theta_0+\theta_1x$  To find out good values for the parameters $\theta_0$ and $\theta_1$ we want to minimize the difference between the calculated result and the actual result of our test data. So we subtract  $h_\theta(x^{(i)})-y^{(i)}$  for all $i$ from $1$ to $m$. Hence we calculate the sum over this difference and then calculate the average by multiplying the sum by $\frac{1}{m}$. So far, so good. This would result in:  $\frac{1}{m}\sum_{i=1}^mh_\theta(x^{(i)})-y^{(i)}$  But this is not what has been suggested. Instead the course suggests to take the square value of the difference, and to multiply by $\frac{1}{2m}$. So the formula is:  $\frac{1}{2m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})^2$  Why is that? Why do we use the square function here, and why do we multiply by $\frac{1}{2m}$ instead of $\frac{1}{m}$?
66423
I understand that floating point calculations have accuracy issues and there are plenty of questions explaining why.  My question is if I run the same calculation twice, can I always rely on it to produce the same result?  What factors might affect this?   Time between calculations? Current state of the CPU? Different hardware? Language / platform / OS? Solar flares?   I have a simple physics simulation and would like to record sessions so that they can be replayed.  If the calculations can be relied on then I should only need to record the initial state plus any user input and I should always be able to reproduce the final state exactly.  If the calculations are not accurate errors at the start may have huge implications by the end of the simulation.  I am currently working in Silverlight though would be interested to know if this question can be answered in general.  Update:  The initial answers indicate yes, but apparently this isn't entirely clear cut as discussed in the comments for the selected answer.  It looks like I will have to do some tests and see what happens.
49648
I often see $I(t)\propto |E(t)|^2$. What is the exact form ? Which constants are missing to make this an equality and why are they so often omitted ?
30426
I want to compare the output of \ref to a number in LaTeX. I thought it would be easy but after spending lots of hours surfing the net I still don't know the answer. I found similar questions on this site but I would prefer a solution that does not require another package to be included, so that I can see clearly how the \ref command (and some others) works. I tried a lot of things, some of which I share below, therefore my question is rather long, I'm sorry about that. First, I tried the following codes (the resulting error messages are shown after a % sign):  \begin{equation} \label{eq:myeq}   a=b   \end{equation}  \ifnum\ref{eq:myeq}=1 true\else false\fi % error: "Missing number, treated as zero."  \expandafter\ifnum\ref{eq:myeq}=1 true\else false\fi % error: "Missing number, treated as zero."  \edef\blabla{\ref{eq:myeq}} % error: Incomplete \iffalse; all text was ignored after line 42. \ifnum\blabla=1 true\else false\fi \expandafter\ifnum\blabla=1 true\else false\fi   Of course, \ref{eq:myeq} gave the expected result, namely 1.  Then, I found the definition of \ref using the latexdef ref command:       \ref:   macro:#1->\expandafter \@setref \csname r@#1\endcsname \@firstoftwo {#1}   Further examining \@setref and \firstoftwo, I concluded that the equation number (together with the page number) is stored in a control sequence called \r@eq:myeq. More precisely, the content of \r@eq:myeq is {1}{1} where the first number is the equation number and the second number is the page number. (The output of \expandafter{\csname r@eq:myeq\endcsname} is 11.) If I understand well, in order to obtain the equation number we have to expand \csname r@eq:myeq\endcsname by only two levels to get {1}{1} (the first level of expansion yields \r@eq:myeq), and then we choose the first argument by \@firstoftwo. Since there is no simple way to expand a control sequence by only one level, it is done in a rather circuitous (say tricky) way by \ref. This I could also reproduce by the following code:  \def\sth#1#2{\expandafter #2#1} \makeatletter \expandafter\sth\csname r@eq:myeq\endcsname\@firstoftwo \quad \ifnum1=\expandafter\sth\csname r@eq:myeq\endcsname\@firstoftwo true\else false\fi \makeatother   which resulted in 1   true. So far so good but there are still a few questions. First of all, if the above code works then why does the code with \ifnum1=\ref{eq:myeq}... fail (see above)? Seemingly, the two "algorithms" are doing the same. Second, the above code works only if the auxiliary file (with aux extension) contains the label definition. If it doesn't (the reference is undefined) then LaTeX gives the following error message: Missing number, treated as zero. How can I write a code that works even if the reference is undefined? Thank everybody who read all my above struggle and I would appreciate if someone shed light on the points that were not clear to me and who corrected me where I was wrong and confirmed where I was correct. Unfortunately the whole command syntax of (La)TeX is still very strange to me.
119980
Let's say I have a matrix like below: $$ W = \begin{bmatrix} w_{1,1} &amp; w_{1,2} \\ w_{2,1} &amp; w_{2,2} \end{bmatrix} $$ $$ \vec{x} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} $$ $$ \vec{y} = W\vec{x} = \begin{bmatrix} w_{1,1}x_1 + w_{1,2}x_2 \\ w_{2,1}x_1 + w_{2,2}x_2 \end{bmatrix} $$ In back-propagation, it is needed to calculate the $\partial {\vec{y}} \over \partial {W}$ to update  $W$. But, according to , there is no consensus about the definition of a derivative of a vector by a matrix. Then, how can I get the value of $\partial {\vec{y}} \over \partial {W}$?