Commit d508f367 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

Compute the binomial coefficient for non-integer values.

parent 6632aaa9
function b = binomial(p, k)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
arguments
p (1,1) double;
k (1,1) double {mustBeInteger};
end
if p >= 0
b = gamma(p+1) / gamma(k+1) / gamma(p-k + 1);
else
b = (-1)^k * misc.binomial( -p + k -1, k);
end
end
......@@ -4,6 +4,30 @@ function [tests] = testMisc()
tests = functiontests(localfunctions());
end
function testBinomial(testCase)
for k = 1:3
for n = k:k+3
% test the integer order
b1 = nchoosek(n, k);
b2 = misc.binomial(n, k);
testCase.verifyEqual(b1, b2);
end
end
% test the non integer case - example from wikipedia
testCase.verifyEqual( misc.binomial(2.5, 2), 1.875)
testCase.verifyEqual( misc.binomial(-1, 4), (-1)^4)
for k = 1:3
% test non integer over zero
testCase.verifyEqual( misc.binomial( k*0.3, 0), 1);
% test non integer over one
testCase.verifyEqual( misc.binomial( k*0.3, 1), k * 0.3, 'AbsTol', 1e-16);
end
end
function testUnique4cells(tc)
Moriginal = {zeros(2), 0, rand(3), [0, 1; 0, 0], "asdf", 0, 'asdf', [0, 1; 0, 0], eps, eps};
Mexpected = {zeros(2), 0, Moriginal{3}, [0, 1; 0, 0], "asdf", eps};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment