Pullback metric "Hello world"
-------TO----->
%%Paste commands into MATLAB window or download script
clear all; clc; close all;
%% Consider the set of points on the real line
x = -3e1:0.1:3e1;
y = zeros(length(x)); %just for plotting in 2d
%% Lets have a look
figure(1)
plot(x,y,'rx');
hold on
plot(x(1),y(1),'bo','Linewidth',5);
hold on
plot(x(end),y(end),'go','Linewidth',5);
%print(gcf, '-dpdf', '-r600', 'real line')
%% The euclidean distance metric `d' between two points in the set is |x1-x2|
func_d_normal = @(x1,x2)(d(x1,x2));
%% lets see, the distance between -30 and 30 should be 60..
test_d_euclidean = func_d_normal(x(1),x(end))
%% Now define a bijective function `f' which maps R to R^2,
%% and a `pullback' distance metric as follows:
func_d_pullback = @(x1,x2)(d2( f(x1), f(x2) ));
%% lets test it out
test_d2_pullback = func_d_pullback(x(1),x(end))
%% Lets have a look at the bijective mapping
x_new = f(x');
figure(2)
plot(x_new(:,1),x_new(:,2),'ro');
hold on
plot(x_new(1,1),x_new(1,2),'bo','Linewidth',5);
hold on
plot(x_new(end,1),x_new(end,2),'go','Linewidth',5);
print(gcf, '-dpdf', '-r600', 'mapped line')
%% Plot pairwise distance matrix between point in the set `x'
figure(3)
D = pdist2(x',x',func_d_normal); contourf(x,x,D,20); axis equal, colorbar; axis([min(x) max(x) min(x) max(x)])
hold on, plot([min(x) max(x)],[0 0],'k')
hold on, plot([0 0 ],[min(x) max(x)],'k')
%print(gcf, '-dpdf', '-r600', 'dist_matrix_euclidean')
%% Plot pairwise `pullback' distance matrix between point in the set `x'
figure(4)
D2 = pdist2(x',x',func_d_pullback); contourf(x,x,D2,8); axis equal, colorbar; axis([min(x) max(x) min(x) max(x)])
hold on, plot([min(x) max(x)],[0 0],'k')
hold on, plot([0 0 ],[min(x) max(x)],'k')
%print(gcf, '-dpdf', '-r600', 'dist_matrix_pullback')
% Reference: https://www.youtube.com/watch?v=qVA6w29H2QU