Rank: 6 based on 2819 downloads (last 30 days) and 52 files submitted
photo

John D'Errico

E-mail
Company/University
Retired

Personal Profile:

Mainly retired from Eastman Kodak. (Of course, Kodak itself is now semi-retired. I don't think I had any influence in that.) I still write MATLAB code as I find something interesting, but I DON'T answer your questions, and I do NOT do homework. Your homework is YOUR problem, not mine. Do NOT e-mail me with your homework problems or student projects. When I'm not doing something with MATLAB, you might find me playing bridge.

Professional Interests:
Bridge, MATLAB, numerical analysis, mathematical modeling

 

Watch this Author's files

 

Files Posted by John View all
Updated   File Tags Downloads
(last 30 days)
Comments Rating
31 Jul 2013 Variable Precision Integer Arithmetic Arithmetic with integers of fully arbitrary size. Arrays and vectors of vpi numbers are supported. Author: John D'Errico integer, biginteger, arithmetic, operator, overloading, long 107 111
  • 4.81818
4.8 | 37 ratings
31 Jul 2013 nearestSPD Finding the nearest positive definite matrix Author: John D'Errico positive definite mat..., covariance matrix, covariance, spd, statistics 22 2
27 Feb 2013 Screenshot distance2curve Find the closest point on a (n-dimensional) curve to any given point or set of points Author: John D'Errico arclength, interpolant, distance, interpolation, curve, space curve 56 7
  • 5.0
5.0 | 4 ratings
02 Jan 2013 Inverse (integrated) gradient Vectorized & fully sparse 1-d, 2-d, & 3-d integrated gradients Author: John D'Errico integration, simpsons rule, gradient, cumulative, trapezoidal rule, numerical integration 27 5
  • 5.0
5.0 | 4 ratings
10 Dec 2012 HPF - a big decimal class High precision floating point arithmetic, a new class written in MATLAB Author: John D'Errico hpf, arithmetic, big decimal, floating point, multiple precision, mathematics 30 5
  • 5.0
5.0 | 6 ratings
Comments and Ratings by John View all
Updated File Comments Rating
31 Jul 2013 Variable Precision Integer Arithmetic Arithmetic with integers of fully arbitrary size. Arrays and vectors of vpi numbers are supported. Author: John D'Errico

Oops. Fixed totient(1) & uploaded.

31 Jul 2013 nearestSPD Finding the nearest positive definite matrix Author: John D'Errico

Sorry about that. Frobenius norm is minimized.

29 Jul 2013 Inhull Efficient test for points inside a convex hull in n dimensions Author: John D'Errico

Daniella - Sorry, but I really try not to do consulting in the comments, so I won't go any further with this. But ask yourself what you would expect to happen if the three points fell exactly in a straight line?

23 Jul 2013 Inhull Efficient test for points inside a convex hull in n dimensions Author: John D'Errico

I KNEW you were going to ask this question, and I'm quite sure I'm not even psychic. :)

Consider three points in R^3, here as rows of X. Clearly they form a triangle. Do the points as rows of Y lie inside that triangle?

X = [1 0 0;0 1 0;0 0 1];
Y = [1 1 1;.25 -.25 1;.2 .3 .5;4/7 2/7 1/7]

Are the points in Y in the plane of the triangle at all? A plane is defined by a point on the plane, and the normal vector to that plane. We can also define the plane in terms of a point on the plane, and a pair of basis vectors spanning the plane. In fact, we will use both of these forms.

First, choose a point (P) on the plane. Here P=X(1,:) will obviously suffice. Translate the problem so that the point on the plane is the origin. Essentially this means subtracting P from the rows of both X and of Y.

P = X(1,:);
XP = bsxfun(@minus,X,P)
YP = bsxfun(@minus,Y,P)

XP =
0 0 0
-1 1 0
-1 0 1

YP =
0 1 1
-0.75 -0.25 1
-0.8 0.3 0.5
-0.42857 0.28571 0.14286

Now, compute the normal vector to the plane of the triangle. Since this is a three dimensional problem, I COULD do it using the cross product tool found in MATLAB (as cross), but perhaps a more general solution comes from the function null.

N = cross(XP(2,:),XP(3,:))
N =
1 1 1

N = null(XP)
N =
0.5774
0.5774
0.5774

In fact, both vectors are essentially the same, although one is transposed compared to the other, and the latter is normalized to have unit norm. Both aspects are irrelevant here. I'll use the result from null to do my tests.

The test for being in-plane is now trivial. If the result of YP*N is zero (to within floating point trash) then the corresponding point is in the plane of the triangle. BE VERY CAREFUL HERE, as unless you take care in the test for zero, it will fail. You need to use an intelligent tolerance.

YP*N
ans =
1.1547
0
0
-4.1633e-17

OK, so points 2 and 3 are in the plane of the triangle. In fact, point 4 is also in the plane of the triangle, but I chose it so that you would see that a tolerance is needed here. A reasonable tolerance might be:

tol = 10*eps(sqrt(sum(YP.*YP,2)))
tol =
2.2204e-15
2.2204e-15
1.1102e-15
1.1102e-15

That tolerance was chosen because N is a UNIT normal vector, so we need only consider the sizes of the numbers in YP.

inplane = find(abs(YP*N) < tol)
inplane =
2
3
4

So we can see that points [2,3,4] are in the plane of the triangle.

Are those points actually inside the triangle? For this, we need to project the problem into the plane of the triangle. Basis vectors that span the plane of the triangle are given in B:

B = null(N')
B =
-0.57735 -0.57735
0.78868 -0.21132
-0.21132 0.78868

XPProj = XP*B
XPProj =
0 0
1.366 0.36603
0.36603 1.366

YPProj = YP(inplane,:)*B
YPProj =
0.024519 1.2745
0.59282 0.79282
0.44258 0.29973

Here I've dropped out point 1, because it was of no interest as it lies out of plane. The rows of XPProj define a triangle in the subspace of our plane. Do the points as rows of YPProj lie in that triangle?

inplane(inhull(YPProj,XPProj))
ans =
3
4

So inhull tells us that points 3 and 4 did lie inside the triangle. In fact, inpolygon tells us the same thing.

inplane(inpolygon(YPProj(:,1),YPProj(:,2),XPProj(:,1),XPProj(:,2)))
ans =
3
4

Fairly long winded, but it comes down to only a few short lines of code.

22 Jul 2013 Inhull Efficient test for points inside a convex hull in n dimensions Author: John D'Errico

Daniella - I'm sorry, but 3 points forms a TRIANGLE. A triangle is not a closed surface in 3 dimensions.

If you have a point in 3-d and wish to test to see if it actually lies inside a triangle, then you would first need to test to see if it even lies inside the plane of the triangle. If it does lie in that plane, then you would need to convert the problem to a 2-d problem by projecting it all into the 2-d subspace defined by the plane of the triangle. Once you have done so, then inpolygon would do as well as inhull for a triangle, as long as you have done the projection into 2-d.

Comments and Ratings on John's Files View all
Updated File Comment by Comments Rating
31 Jul 2013 Variable Precision Integer Arithmetic Arithmetic with integers of fully arbitrary size. Arrays and vectors of vpi numbers are supported. Author: John D'Errico D'Errico, John

Oops. Fixed totient(1) & uploaded.

31 Jul 2013 Variable Precision Integer Arithmetic Arithmetic with integers of fully arbitrary size. Arrays and vectors of vpi numbers are supported. Author: John D'Errico cyclist, the

Great submission. I use it rarely, but it is a life-saver when I need it.

I did find one foible, I believe. Your totient function outputs totient(1) as 0, but both the Wikipedia page you cite and the Sloane Online Encyclopedia of Integer Sequences (http://oeis.org/A000010) say that totient(1) = 1.

31 Jul 2013 nearestSPD Finding the nearest positive definite matrix Author: John D'Errico D'Errico, John

Sorry about that. Frobenius norm is minimized.

31 Jul 2013 nearestSPD Finding the nearest positive definite matrix Author: John D'Errico Pošík, Petr

Hi John. I miss in the description how the "nearness" of the 2 matrices, U and Uj, is measured. Could you comment on that? Thanks, Petr

29 Jul 2013 Inhull Efficient test for points inside a convex hull in n dimensions Author: John D'Errico D'Errico, John

Daniella - Sorry, but I really try not to do consulting in the comments, so I won't go any further with this. But ask yourself what you would expect to happen if the three points fell exactly in a straight line?

Top Tags Applied by John
interpolation, curve, distance, modeling, regression
Files Tagged by John View all
Updated   File Tags Downloads
(last 30 days)
Comments Rating
31 Jul 2013 Variable Precision Integer Arithmetic Arithmetic with integers of fully arbitrary size. Arrays and vectors of vpi numbers are supported. Author: John D'Errico integer, biginteger, arithmetic, operator, overloading, long 107 111
  • 4.81818
4.8 | 37 ratings
31 Jul 2013 nearestSPD Finding the nearest positive definite matrix Author: John D'Errico positive definite mat..., covariance matrix, covariance, spd, statistics 22 2
27 Feb 2013 Screenshot distance2curve Find the closest point on a (n-dimensional) curve to any given point or set of points Author: John D'Errico arclength, interpolant, distance, interpolation, curve, space curve 56 7
  • 5.0
5.0 | 4 ratings
02 Jan 2013 Inverse (integrated) gradient Vectorized & fully sparse 1-d, 2-d, & 3-d integrated gradients Author: John D'Errico integration, simpsons rule, gradient, cumulative, trapezoidal rule, numerical integration 27 5
  • 5.0
5.0 | 4 ratings
10 Dec 2012 HPF - a big decimal class High precision floating point arithmetic, a new class written in MATLAB Author: John D'Errico hpf, arithmetic, big decimal, floating point, multiple precision, mathematics 30 5
  • 5.0
5.0 | 6 ratings

Contact us