Repository navigation
Expand file tree
/
Copy pathot.lp.html
More file actions
1636 lines (1577 loc) · 165 KB
/
Copy pathot.lp.html
File metadata and controls
1636 lines (1577 loc) · 165 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ot.lp — POT Python Optimal Transport 0.9.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=9edc463e" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery.css?v=fd6457a4" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-binder.css?v=f4aeca0c" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-dataframe.css?v=2082cf3c" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-rendered-html.css?v=1277b6f3" />
<script src="../_static/jquery.js?v=5d32c60e"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../_static/documentation_options.js?v=f2328ca0"></script>
<script src="../_static/doctools.js?v=fd6eb6e6"></script>
<script src="../_static/sphinx_highlight.js?v=6ffebe34"></script>
<script src="../_static/sg-tags.js?v=d80f6fa4"></script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="ot.mapping" href="ot.mapping.html" />
<link rel="prev" title="ot.lowrank" href="ot.lowrank.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home">
POT Python Optimal Transport
<img src="../_static/logo_dark.svg" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">POT: Python Optimal Transport</a></li>
<li class="toctree-l1"><a class="reference internal" href="../auto_examples/plot_quickstart_guide.html">Quickstart Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../auto_examples/index.html">Examples gallery</a></li>
<li class="toctree-l1"><a class="reference internal" href="../user_guide.html">User guide</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../all.html">API and modules</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="ot.backend.html">ot.backend</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.batch.html">ot.batch</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.bregman.html">ot.bregman</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.bsp.html">ot.bsp</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.coot.html">ot.coot</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.da.html">ot.da</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.datasets.html">ot.datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.dr.html">ot.dr</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.factored.html">ot.factored</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.gaussian.html">ot.gaussian</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.gmm.html">ot.gmm</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.gnn.html">ot.gnn</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.gromov.html">ot.gromov</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.lowrank.html">ot.lowrank</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">ot.lp</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.NorthWestMMGluing"><code class="docutils literal notranslate"><span class="pre">NorthWestMMGluing()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.barycenter"><code class="docutils literal notranslate"><span class="pre">barycenter()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.binary_search_circle"><code class="docutils literal notranslate"><span class="pre">binary_search_circle()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.check_number_threads"><code class="docutils literal notranslate"><span class="pre">check_number_threads()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.dmmot_monge_1dgrid_loss"><code class="docutils literal notranslate"><span class="pre">dmmot_monge_1dgrid_loss()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.dmmot_monge_1dgrid_optimize"><code class="docutils literal notranslate"><span class="pre">dmmot_monge_1dgrid_optimize()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd"><code class="docutils literal notranslate"><span class="pre">emd()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd2"><code class="docutils literal notranslate"><span class="pre">emd2()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd2_1d"><code class="docutils literal notranslate"><span class="pre">emd2_1d()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd2_lazy"><code class="docutils literal notranslate"><span class="pre">emd2_lazy()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd_1d"><code class="docutils literal notranslate"><span class="pre">emd_1d()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd_1d_dual_backprop"><code class="docutils literal notranslate"><span class="pre">emd_1d_dual_backprop()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.emd_1d_sorted"><code class="docutils literal notranslate"><span class="pre">emd_1d_sorted()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.free_support_barycenter"><code class="docutils literal notranslate"><span class="pre">free_support_barycenter()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.free_support_barycenter_generic_costs"><code class="docutils literal notranslate"><span class="pre">free_support_barycenter_generic_costs()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.generalized_free_support_barycenter"><code class="docutils literal notranslate"><span class="pre">generalized_free_support_barycenter()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.linear_circular_ot"><code class="docutils literal notranslate"><span class="pre">linear_circular_ot()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.ot_barycenter_energy"><code class="docutils literal notranslate"><span class="pre">ot_barycenter_energy()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.semidiscrete_wasserstein2_unif_circle"><code class="docutils literal notranslate"><span class="pre">semidiscrete_wasserstein2_unif_circle()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.wasserstein_1d"><code class="docutils literal notranslate"><span class="pre">wasserstein_1d()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#ot.lp.wasserstein_circle"><code class="docutils literal notranslate"><span class="pre">wasserstein_circle()</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="ot.mapping.html">ot.mapping</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.optim.html">ot.optim</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.partial.html">ot.partial</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.plot.html">ot.plot</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.regpath.html">ot.regpath</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.sliced.html">ot.sliced</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.sgot.html">ot.sgot</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.smooth.html">ot.smooth</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.stochastic.html">ot.stochastic</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.unbalanced.html">ot.unbalanced</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.utils.html">ot.utils</a></li>
<li class="toctree-l2"><a class="reference internal" href="ot.weak.html">ot.weak</a></li>
<li class="toctree-l2"><a class="reference internal" href="../all.html#module-ot">Main <code class="xref py py-mod docutils literal notranslate"><span class="pre">ot</span></code> functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../releases.html">Releases</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributors.html">Contributors</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">Contributing to POT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../code_of_conduct.html">Code of conduct</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">POT Python Optimal Transport</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../all.html">API and modules</a></li>
<li class="breadcrumb-item active">ot.lp</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/gen_modules/ot.lp.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="module-ot.lp">
<span id="ot-lp"></span><h1>ot.lp<a class="headerlink" href="#module-ot.lp" title="Link to this heading"></a></h1>
<p>Solvers for the original linear program OT problem.</p>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.NorthWestMMGluing">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">NorthWestMMGluing</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pi_list</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nx</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/_barycenter_solvers.html#NorthWestMMGluing"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.NorthWestMMGluing" title="Link to this definition"></a></dt>
<dd><p>Glue transport plans <span class="math notranslate nohighlight">\((\pi_1, ..., \pi_K)\)</span> which have a common first
marginal using the (multi-marginal) North-West Corner method. Writing the
marginals of each <span class="math notranslate nohighlight">\(\pi_k\in \mathbb{R}^{n\times n_l}\)</span> as <span class="math notranslate nohighlight">\(a \in
\mathbb{R}^n\)</span> and <span class="math notranslate nohighlight">\(b_k \in \mathbb{R}^{n_k}\)</span>, the output represents a
particular K-marginal transport plan <span class="math notranslate nohighlight">\(\rho \in
\mathbb{R}^{n_1\times\cdots\times n_K}\)</span> whose k-th marginal is <span class="math notranslate nohighlight">\(b_k\)</span>.
This K-plan is such that there exists a K+1-marginal transport plan
<span class="math notranslate nohighlight">\(\gamma \in \mathbb{R}^{n\times n_1 \times \cdots \times n_K}\)</span> such
that <span class="math notranslate nohighlight">\(\sum_i\gamma_{i,j_1,\cdots,j_K} = \rho_{j_1, \cdots, j_K}\)</span> and
with Einstein summation convention, <span class="math notranslate nohighlight">\(\gamma_{i, j_1, \cdots, j_K} =
[\pi_k]_{i, j_k}\)</span> for all <span class="math notranslate nohighlight">\(k=1,\cdots,K\)</span>.</p>
<p>Instead of outputting the full K-multi-marginal plan <span class="math notranslate nohighlight">\(\rho\)</span>, this
function provides an array <cite>J</cite> of shape (N, K) where each <cite>J[i]</cite> is of the
form <cite>(J[i, 1], …, J[i, K])</cite> with each <cite>J[i, k]</cite> between 0 and
<span class="math notranslate nohighlight">\(n_k-1\)</span>, and a weight vector <cite>w</cite> of size N, such that the K-plan
<span class="math notranslate nohighlight">\(\rho\)</span> writes:</p>
<div class="math notranslate nohighlight">
\[\rho_{j_1, \cdots, j_K} = \mathbb{1}\left(\exists i \text{ s.t. } (j_1, \cdots, j_K) = (J[i, 1], \cdots, J[i, K])\right)\ w_i.\]</div>
<p>This representation is useful for its memory efficiency, as it avoids
storing the full K-marginal plan.</p>
<p>If <cite>log=True</cite>, the function computes the full K+1-marginal transport plan
<span class="math notranslate nohighlight">\(\gamma\)</span> and stores it in log_dict[‘gamma’]. Note that this option is
extremely costly in memory.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>pi_list</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.14)"><em>list</em></a><em> of </em><em>arrays</em><em> (</em><em>n</em><em>, </em><em>n_k</em><em>)</em>) – List of transport plans.</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, return a log dictionary (computationally expensive).</p></li>
<li><p><strong>a</strong> (<em>array</em><em> (</em><em>n</em><em>,</em><em>)</em><em>, </em><em>optional</em>) – The common first marginal of each transport plan. If None is provided,
it is computed as the first marginal of the first transport plan.</p></li>
<li><p><strong>nx</strong> (<a class="reference internal" href="ot.utils.html#id32" title="ot.utils.BaryResult.backend"><em>backend</em></a><em>, </em><em>optional</em>) – The backend to use. If None is provided, the backend of the first
transport plan is used.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>J</strong> (<em>array (N, K)</em>) – The indices (J[i, 1], …, J[i, K]) of the K-plan <span class="math notranslate nohighlight">\(\rho\)</span>.</p></li>
<li><p><strong>w</strong> (<em>array (N,)</em>) – The weights w_i of the K-plan <span class="math notranslate nohighlight">\(\rho\)</span>.</p></li>
<li><p><strong>log_dict</strong> (<em>dict, optional</em>) – If log=True, a dictionary containing the full K+1-marginal transport
plan under the key ‘gamma’.</p></li>
</ul>
</p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.barycenter">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">barycenter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">M</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">weights</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">solver</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'highs-ipm'</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/_barycenter_solvers.html#barycenter"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.barycenter" title="Link to this definition"></a></dt>
<dd><p>Compute the Wasserstein barycenter of distributions A</p>
<blockquote>
<div><p>The function solves the following optimization problem [16]:</p>
</div></blockquote>
<div class="math notranslate nohighlight">
\[\mathbf{a} = arg\min_\mathbf{a} \sum_i W_{1}(\mathbf{a},\mathbf{a}_i)\]</div>
<p>where :</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(W_1(\cdot,\cdot)\)</span> is the Wasserstein distance (see ot.emd.sinkhorn)</p></li>
<li><p><span class="math notranslate nohighlight">\(\mathbf{a}_i\)</span> are training distributions in the columns of matrix <span class="math notranslate nohighlight">\(\mathbf{A}\)</span></p></li>
</ul>
<p>The linear program is solved using the interior point solver from scipy.optimize.
If cvxopt solver if installed it can use cvxopt</p>
<p>Note that this problem do not scale well (both in memory and computational time).</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>A</strong> (<em>np.ndarray</em><em> (</em><em>d</em><em>,</em><em>n</em><em>)</em>) – n training distributions a_i of size d</p></li>
<li><p><strong>M</strong> (<em>np.ndarray</em><em> (</em><em>d</em><em>,</em><em>d</em><em>)</em>) – loss matrix for OT</p></li>
<li><p><strong>reg</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a>) – Regularization term >0</p></li>
<li><p><strong>weights</strong> (<em>np.ndarray</em><em> (</em><em>n</em><em>,</em><em>)</em>) – Weights of each histogram a_i on the simplex (barycentric coordinates)</p></li>
<li><p><strong>verbose</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – Print information along iterations</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – record log if True</p></li>
<li><p><strong>solver</strong> (<em>string</em><em>, </em><em>optional</em>) – the solver used, default ‘interior-point’ use the lp solver from
scipy.optimize. None, or ‘glpk’ or ‘mosek’ use the solver from cvxopt.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>a</strong> (<em>(d,) ndarray</em>) – Wasserstein barycenter</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – log dictionary return only if log==True in parameters</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>16<span class="fn-bracket">]</span></span>
<p>Agueh, M., & Carlier, G. (2011). Barycenters in the Wasserstein space. SIAM Journal on Mathematical Analysis, 43(2), 904-924.</p>
</aside>
</aside>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.binary_search_circle">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">binary_search_circle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">u_values</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v_values</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">u_weights</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v_weights</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Lm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Lp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1e-06</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">require_sort</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/solver_circle.html#binary_search_circle"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.binary_search_circle" title="Link to this definition"></a></dt>
<dd><p>Computes the Wasserstein distance on the circle using the Binary search algorithm proposed in [44].
Samples need to be in <span class="math notranslate nohighlight">\(S^1\cong [0,1[\)</span>. If they are on <span class="math notranslate nohighlight">\(\mathbb{R}\)</span>,
takes the value modulo 1.
If the values are on <span class="math notranslate nohighlight">\(S^1\subset\mathbb{R}^2\)</span>, it is required to first find the coordinates
using e.g. the atan2 function.</p>
<div class="math notranslate nohighlight">
\[W_p^p(u,v) = \inf_{\theta\in\mathbb{R}}\int_0^1 |F_u^{-1}(q) - (F_v-\theta)^{-1}(q)|^p\ \mathrm{d}q\]</div>
<p>where:</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(F_u\)</span> and <span class="math notranslate nohighlight">\(F_v\)</span> are respectively the cdfs of <span class="math notranslate nohighlight">\(u\)</span> and <span class="math notranslate nohighlight">\(v\)</span></p></li>
</ul>
<p>For values <span class="math notranslate nohighlight">\(x=(x_1,x_2)\in S^1\)</span>, it is required to first get their coordinates with</p>
<div class="math notranslate nohighlight">
\[u = \frac{\pi + \mathrm{atan2}(-x_2,-x_1)}{2\pi}\]</div>
<p>using e.g. ot.utils.get_coordinate_circle(x)</p>
<p>The function runs on backend but tensorflow and jax are not supported.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>u_values</strong> (<em>ndarray</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em>) – samples in the source domain (coordinates on [0,1[)</p></li>
<li><p><strong>v_values</strong> (<em>ndarray</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em>) – samples in the target domain (coordinates on [0,1[)</p></li>
<li><p><strong>u_weights</strong> (<em>ndarray</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em><em>, </em><em>optional</em>) – samples weights in the source domain</p></li>
<li><p><strong>v_weights</strong> (<em>ndarray</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em><em>, </em><em>optional</em>) – samples weights in the target domain</p></li>
<li><p><strong>p</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em><em> (</em><em>default=1</em><em>)</em>) – Power p used for computing the Wasserstein distance</p></li>
<li><p><strong>Lm</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em>) – Lower bound dC</p></li>
<li><p><strong>Lp</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em>) – Upper bound dC</p></li>
<li><p><strong>tm</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em>) – Lower bound theta</p></li>
<li><p><strong>tp</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em>) – Upper bound theta</p></li>
<li><p><strong>eps</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em>) – Stopping condition</p></li>
<li><p><strong>require_sort</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, sort the values.</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, returns also the optimal theta</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>loss</strong> (<em>float/array-like, shape (…)</em>) – Batched cost associated to the optimal transportation</p></li>
<li><p><strong>log</strong> (<em>dict, optional</em>) – log dictionary returned only if log==True in parameters</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.2</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.8</span><span class="p">]])</span><span class="o">%</span><span class="mi">1</span>
<span class="gp">>>> </span><span class="n">v</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.4</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.7</span><span class="p">]])</span><span class="o">%</span><span class="mi">1</span>
<span class="gp">>>> </span><span class="n">binary_search_circle</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">v</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([0.1])</span>
</pre></div>
</div>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>44<span class="fn-bracket">]</span></span>
<p>Delon, Julie, Julien Salomon, and Andrei Sobolevski. “Fast transport optimization for Monge costs on the circle.” SIAM Journal on Applied Mathematics 70.7 (2010): 2239-2258.</p>
</aside>
</aside>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.check_number_threads">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">check_number_threads</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">numThreads</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/utils.html#check_number_threads"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.check_number_threads" title="Link to this definition"></a></dt>
<dd><p>Checks whether or not the requested number of threads has a valid value.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>numThreads</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em> or </em><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.14)"><em>str</em></a>) – The requested number of threads, should either be a strictly positive integer or “max” or None</p>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><strong>numThreads</strong> – Corrected number of threads</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.dmmot_monge_1dgrid_loss">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">dmmot_monge_1dgrid_loss</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/dmmot.html#dmmot_monge_1dgrid_loss"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.dmmot_monge_1dgrid_loss" title="Link to this definition"></a></dt>
<dd><p>Compute the discrete multi-marginal optimal transport of distributions A.</p>
<p>This function operates on distributions whose supports are real numbers on
the real line.</p>
<p>The algorithm solves both primal and dual d-MMOT programs concurrently to
produce the optimal transport plan as well as the total (minimal) cost.
The cost is a ground cost, and the solution is independent of
which Monge cost is desired.</p>
<p>The algorithm accepts <span class="math notranslate nohighlight">\(d\)</span> distributions (i.e., histograms)
<span class="math notranslate nohighlight">\(a_{1}, \ldots, a_{d} \in \mathbb{R}_{+}^{n}\)</span> with <span class="math notranslate nohighlight">\(e^{\prime}
a_{j}=1\)</span> for all <span class="math notranslate nohighlight">\(j \in[d]\)</span>. Although the algorithm states that all
histograms have the same number of bins, the algorithm can be easily
adapted to accept as inputs <span class="math notranslate nohighlight">\(a_{i} \in \mathbb{R}_{+}^{n_{i}}\)</span>
with <span class="math notranslate nohighlight">\(n_{i} \neq n_{j}\)</span> [50].</p>
<p>The function solves the following optimization problem[51]:</p>
<div class="math notranslate nohighlight">
\[\begin{split}\begin{align}\begin{aligned}
\underset{\gamma\in\mathbb{R}^{n^{d}}_{+}} {\textrm{min}}
\sum_{i_1,\ldots,i_d} c(i_1,\ldots, i_d)\, \gamma(i_1,\ldots,i_d)
\quad \textrm{s.t.}
\sum_{i_2,\ldots,i_d} \gamma(i_1,\ldots,i_d) &= a_1(i_i),
(\forall i_1\in[n])\\
\qquad\vdots\\
\sum_{i_1,\ldots,i_{d-1}} \gamma(i_1,\ldots,i_d) &= a_{d}(i_{d}),
(\forall i_d\in[n]).
\end{aligned}
\end{align}\end{split}\]</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>A</strong> (<em>nx.ndarray</em><em>, </em><em>shape</em><em> (</em><em>dim</em><em>, </em><em>n_hists</em><em>)</em>) – The input ndarray containing distributions of n bins in d dimensions.</p></li>
<li><p><strong>verbose</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, print debugging information during execution. Default=False.</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, record log. Default is False.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>obj</strong> (<em>float</em>) – the value of the primal objective function evaluated at the solution.</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – A dictionary containing the log of the discrete mmot problem:
- ‘A’: a dictionary that maps tuples of indices to the corresponding
primal variables. The tuples are the indices of the entries that are
set to their minimum value during the algorithm.
- ‘primal objective’: a float, the value of the objective function
evaluated at the solution.
- ‘dual’: a list of arrays, the dual variables corresponding to
the input arrays. The i-th element of the list is the dual variable
corresponding to the i-th dimension of the input arrays.
- ‘dual objective’: a float, the value of the dual objective function
evaluated at the solution.</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>55<span class="fn-bracket">]</span></span>
<p>Ronak Mehta, Jeffery Kline, Vishnu Suresh Lokhande, Glenn Fung, &
Vikas Singh (2023). Efficient Discrete Multi Marginal Optimal
Transport Regularization. In The Eleventh International
Conference on Learning Representations.</p>
</aside>
<aside class="footnote brackets" id="id4" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>56<span class="fn-bracket">]</span></span>
<p>Jeffery Kline. Properties of the d-dimensional earth mover’s
problem. Discrete Applied Mathematics, 265: 128-141, 2019.</p>
</aside>
<aside class="footnote brackets" id="id5" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>58<span class="fn-bracket">]</span></span>
<p>Leonid V Kantorovich. On the translocation of masses. Dokl. Akad.
Nauk SSSR, 37:227-229, 1942.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#ot.lp.dmmot_monge_1dgrid_optimize" title="ot.lp.dmmot_monge_1dgrid_optimize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.dmmot_monge_1dgrid_optimize</span></code></a></dt><dd><p>Optimize the d-Dimensional Earth</p>
</dd>
</dl>
<p><code class="xref py py-obj docutils literal notranslate"><span class="pre">Mover</span></code></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.dmmot_monge_1dgrid_optimize">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">dmmot_monge_1dgrid_optimize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">A</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">niters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lr_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1e-05</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lr_decay</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0.995</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">print_rate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/dmmot.html#dmmot_monge_1dgrid_optimize"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.dmmot_monge_1dgrid_optimize" title="Link to this definition"></a></dt>
<dd><p>Minimize the d-dimensional EMD using gradient descent.</p>
<p>Discrete Multi-Marginal Optimal Transport (d-MMOT): Let <span class="math notranslate nohighlight">\(a_1, \ldots,
a_d\in\mathbb{R}^n_{+}\)</span> be discrete probability distributions. Here,
the d-MMOT is the LP,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\begin{align}\begin{aligned}
\underset{x\in\mathbb{R}^{n^{d}}_{+}} {\textrm{min}}
\sum_{i_1,\ldots,i_d} c(i_1,\ldots, i_d)\, x(i_1,\ldots,i_d) \quad
\textrm{s.t.}
\sum_{i_2,\ldots,i_d} x(i_1,\ldots,i_d) &= a_1(i_i),
(\forall i_1\in[n])\\
\qquad\vdots\\
\sum_{i_1,\ldots,i_{d-1}} x(i_1,\ldots,i_d) &= a_{d}(i_{d}),
(\forall i_d\in[n]).
\end{aligned}
\end{align}\end{split}\]</div>
<p>The dual linear program of the d-MMOT problem is:</p>
<div class="math notranslate nohighlight">
\[\underset{z_j\in\mathbb{R}^n, j\in[d]}{\textrm{maximize}}\qquad\sum_{j}
a_j'z_j\qquad \textrm{subject to}\qquad z_{1}(i_1)+\cdots+z_{d}(i_{d})
\leq c(i_1,\ldots,i_{d}),\]</div>
<p>where the indices in the constraints include all <span class="math notranslate nohighlight">\(i_j\in[n]\)</span>, :math:
<cite>jin[d]</cite>. Denote by <span class="math notranslate nohighlight">\(\phi(a_1,\ldots,a_d)\)</span>, the optimal objective
value of the LP in d-MMOT problem. Let <span class="math notranslate nohighlight">\(z^*\)</span> be an optimal solution
to the dual program. Then,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\begin{align}
\nabla \phi(a_1,\ldots,a_{d}) &= z^*,
~~\text{and for any $t\in \mathbb{R}$,}~~
\phi(a_1,a_2,\ldots,a_{d}) = \sum_{j}a_j'
(z_j^* + t\, \eta), \nonumber \\
\text{where } \eta &:= (z_1^{*}(n)\,e, z^*_1(n)\,e, \cdots,
z^*_{d}(n)\,e)
\end{align}\end{split}\]</div>
<p>Using these dual variables naturally provided by the algorithm in
ot.lp.dmmot_monge_1dgrid_loss, gradient steps move each input distribution
to minimize their d-mmot distance.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>A</strong> (<em>nx.ndarray</em><em>, </em><em>shape</em><em> (</em><em>dim</em><em>, </em><em>n_hists</em><em>)</em>) – The input ndarray containing distributions of n bins in d dimensions.</p></li>
<li><p><strong>niters</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=100</em><em>)</em>) – The maximum number of iterations for the optimization algorithm.</p></li>
<li><p><strong>lr_init</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em><em> (</em><em>default=1e-5</em><em>)</em>) – The initial learning rate (step size) for the optimization algorithm.</p></li>
<li><p><strong>lr_decay</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em><em> (</em><em>default=0.995</em><em>)</em>) – The learning rate decay rate in each iteration.</p></li>
<li><p><strong>print_rate</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=100</em><em>)</em>) – The rate at which to print the objective value and gradient norm
during the optimization algorithm.</p></li>
<li><p><strong>verbose</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, print debugging information during execution. Default=False.</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – If True, record log. Default is False.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>a</strong> (<em>list of ndarrays, each of shape (n,)</em>) – The optimal solution as a list of n approximate barycenters, each of
length vecsize.</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – log dictionary return only if log==True in parameters</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id6" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>55<span class="fn-bracket">]</span></span>
<p>Ronak Mehta, Jeffery Kline, Vishnu Suresh Lokhande, Glenn Fung, &
Vikas Singh (2023). Efficient Discrete Multi Marginal Optimal
Transport Regularization. In The Eleventh International
Conference on Learning Representations.</p>
</aside>
<aside class="footnote brackets" id="id7" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>60<span class="fn-bracket">]</span></span>
<p>Olvi L Mangasarian and RR Meyer. Nonlinear perturbation of linear
programs. SIAM Journal on Control and Optimization, 17(6):745-752, 1979</p>
</aside>
<aside class="footnote brackets" id="id8" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>59<span class="fn-bracket">]</span></span>
<p>Michael C Ferris and Olvi L Mangasarian. Finite perturbation of
convex programs. Applied Mathematics and Optimization, 23(1):263-273,
1991.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#ot.lp.dmmot_monge_1dgrid_loss" title="ot.lp.dmmot_monge_1dgrid_loss"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.dmmot_monge_1dgrid_loss</span></code></a></dt><dd><p>d-Dimensional Earth Mover’s Solver</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">M</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numItermax</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100000</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">center_dual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numThreads</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_marginals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">potentials_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/_network_simplex.html#emd"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd" title="Link to this definition"></a></dt>
<dd><p>Solves the Earth Movers distance problem and returns the OT matrix</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\gamma = \mathop{\arg \min}_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F\\s.t. \ \gamma \mathbf{1} = \mathbf{a}\\ \gamma^T \mathbf{1} = \mathbf{b}\\ \gamma \geq 0\end{aligned}\end{align} \]</div>
<p>where :</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(\mathbf{M}\)</span> is the metric cost matrix</p></li>
<li><p><span class="math notranslate nohighlight">\(\mathbf{a}\)</span> and <span class="math notranslate nohighlight">\(\mathbf{b}\)</span> are the sample weights</p></li>
</ul>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Note that the <span class="math notranslate nohighlight">\(\mathbf{M}\)</span> matrix in numpy needs to be a C-order
numpy.array in float64 format. It will be converted if not in this
format</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function is backend-compatible and will work on arrays
from all compatible backends. But the algorithm uses the C++ CPU backend
which can lead to copy overhead on GPU arrays.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function will cast the computed transport plan to the data type
of the provided input with the following priority: <span class="math notranslate nohighlight">\(\mathbf{a}\)</span>,
then <span class="math notranslate nohighlight">\(\mathbf{b}\)</span>, then <span class="math notranslate nohighlight">\(\mathbf{M}\)</span> if marginals are not provided.
Casting to an integer tensor might result in a loss of precision.
If this behaviour is unwanted, please make sure to provide a
floating point input.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>An error will be raised if the vectors <span class="math notranslate nohighlight">\(\mathbf{a}\)</span> and <span class="math notranslate nohighlight">\(\mathbf{b}\)</span> do not sum to the same value.</p>
</div>
<p>Uses the algorithm proposed in <a class="reference internal" href="#references-emd"><span class="std std-ref">[1]</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>a</strong> (<em>(</em><em>ns</em><em>,</em><em>) </em><em>array-like</em><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a>) – Source histogram (uniform weight if empty list)</p></li>
<li><p><strong>b</strong> (<em>(</em><em>nt</em><em>,</em><em>) </em><em>array-like</em><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a>) – Target histogram (uniform weight if empty list)</p></li>
<li><p><strong>M</strong> (<em>(</em><em>ns</em><em>,</em><em>nt</em><em>) </em><em>array-like</em><em> or </em><em>sparse matrix</em><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a>) – <p>Loss matrix. Can be:</p>
<ul>
<li><p>Dense array (c-order array in numpy with type float64)</p></li>
<li><p>Sparse matrix in backend’s format (scipy.sparse.coo_matrix for NumPy backend,
torch.sparse_coo_tensor for PyTorch backend, etc.)</p></li>
</ul>
</p></li>
<li><p><strong>numItermax</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=100000</em><em>)</em>) – The maximum number of iterations before stopping the optimization
algorithm if it has not converged.</p></li>
<li><p><strong>log</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns a dictionary containing the cost and dual variables.
Otherwise returns only the optimal transportation matrix.</p></li>
<li><p><strong>center_dual</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, centers the dual potential using function
<code class="xref py py-func docutils literal notranslate"><span class="pre">ot.lp.center_ot_dual()</span></code>.</p></li>
<li><p><strong>numThreads</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em> or </em><em>"max"</em><em>, </em><em>optional</em><em> (</em><em>default=1</em><em>)</em>) – Deprecated compatibility parameter. The network simplex solver no
longer uses OpenMP, so this parameter is ignored.</p></li>
<li><p><strong>check_marginals</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, checks that the marginals mass are equal. If False, skips the
check.</p></li>
<li><p><strong>potentials_init</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.14)"><em>tuple</em></a><em> of </em><em>two arrays</em><em> (</em><em>alpha</em><em>, </em><em>beta</em><em>)</em><em>, </em><em>optional</em><em> (</em><em>default=None</em><em>)</em>) – Warmstart dual potentials to accelerate convergence. Should be a tuple
(alpha, beta) where alpha is shape (ns,) and beta is shape (nt,).
These potentials are used to guide initial pivots in the network simplex.
Typically obtained from a previous EMD solve or Sinkhorn approximation.</p></li>
<li><p><strong>note:</strong> (<em>..</em>) – <p>The solver automatically detects sparse format using the backend’s: <code class="xref py py-meth docutils literal notranslate"><span class="pre">issparse()</span></code> method. For sparse inputs:</p>
<ul>
<li><p>Uses a memory-efficient sparse EMD algorithm</p></li>
<li><p>Returns the transport plan as a sparse matrix in the backend’s format</p></li>
<li><p>Supports scipy.sparse (NumPy), torch.sparse (PyTorch), etc.</p></li>
<li><p>JAX and TensorFlow backends don’t support sparse matrices</p></li>
</ul>
</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>gamma</strong> (<em>array-like or sparse matrix, shape (ns, nt)</em>) – Optimal transportation matrix for the given parameters.</p>
<ul>
<li><p>For dense inputs: returns a dense array</p></li>
<li><p>For sparse inputs: returns a sparse matrix in the backend’s format
(e.g., scipy.sparse.coo_matrix for NumPy, torch.sparse_coo_tensor for PyTorch)</p></li>
</ul>
</li>
<li><p><strong>log</strong> (<em>dict, optional</em>) – If input log is True, a dictionary containing the cost, dual variables,
and exit status.</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Simple example with obvious solution. The function emd accepts lists and
perform automatic conversion to numpy arrays</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">ot</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span><span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">b</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span><span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">M</span><span class="o">=</span><span class="p">[[</span><span class="mf">0.</span><span class="p">,</span><span class="mf">1.</span><span class="p">],[</span><span class="mf">1.</span><span class="p">,</span><span class="mf">0.</span><span class="p">]]</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">M</span><span class="p">)</span>
<span class="go">array([[0.5, 0. ],</span>
<span class="go"> [0. , 0.5]])</span>
</pre></div>
</div>
<p class="rubric" id="references-emd">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id9" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
<p>Bonneel, N., Van De Panne, M., Paris, S., & Heidrich, W. (2011,
December). Displacement interpolation using Lagrangian mass transport.
In ACM Transactions on Graphics (TOG) (Vol. 30, No. 6, p. 158). ACM.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="ot.bregman.html#ot.bregman.sinkhorn" title="ot.bregman.sinkhorn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.bregman.sinkhorn</span></code></a></dt><dd><p>Entropic regularized OT</p>
</dd>
<dt><a class="reference internal" href="ot.optim.html#id0" title="ot.optim.cg"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.optim.cg</span></code></a></dt><dd><p>General regularized OT</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd2">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">M</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">processes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numItermax</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100000</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_matrix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">center_dual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numThreads</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_marginals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">potentials_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/_network_simplex.html#emd2"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd2" title="Link to this definition"></a></dt>
<dd><p>Solves the Earth Movers distance problem and returns the loss</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\min_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F\\s.t. \ \gamma \mathbf{1} = \mathbf{a}\\ \gamma^T \mathbf{1} = \mathbf{b}\\ \gamma \geq 0\end{aligned}\end{align} \]</div>
<p>where :</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(\mathbf{M}\)</span> is the metric cost matrix</p></li>
<li><p><span class="math notranslate nohighlight">\(\mathbf{a}\)</span> and <span class="math notranslate nohighlight">\(\mathbf{b}\)</span> are the sample weights</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function is backend-compatible and will work on arrays
from all compatible backends. But the algorithm uses the C++ CPU backend
which can lead to copy overhead on GPU arrays.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function will cast the computed transport plan and
transportation loss to the data type of the provided input with the
following priority : <span class="math notranslate nohighlight">\(\mathbf{a}\)</span>, then <span class="math notranslate nohighlight">\(\mathbf{b}\)</span>,
then <span class="math notranslate nohighlight">\(\mathbf{M}\)</span> if marginals are not provided.
Casting to an integer tensor might result in a loss of precision.
If this behaviour is unwanted, please make sure to provide a
floating point input.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>An error will be raised if the vectors <span class="math notranslate nohighlight">\(\mathbf{a}\)</span> and <span class="math notranslate nohighlight">\(\mathbf{b}\)</span> do not sum to the same value.</p>
</div>
<p>Uses the algorithm proposed in <a class="reference internal" href="#references-emd2"><span class="std std-ref">[1]</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>a</strong> (<em>(</em><em>ns</em><em>,</em><em>) </em><em>array-like</em><em>, </em><em>float64</em>) – Source histogram (uniform weight if empty list)</p></li>
<li><p><strong>b</strong> (<em>(</em><em>nt</em><em>,</em><em>) </em><em>array-like</em><em>, </em><em>float64</em>) – Target histogram (uniform weight if empty list)</p></li>
<li><p><strong>M</strong> (<em>(</em><em>ns</em><em>,</em><em>nt</em><em>) </em><em>array-like</em><em> or </em><em>sparse matrix</em><em>, </em><em>float64</em>) – <p>Loss matrix. Can be:</p>
<ul>
<li><p>Dense array (c-order array in numpy with type float64)</p></li>
<li><p>Sparse matrix in backend’s format (scipy.sparse.coo_matrix for NumPy backend,
torch.sparse_coo_tensor for PyTorch backend, etc.)</p></li>
</ul>
</p></li>
<li><p><strong>processes</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=1</em><em>)</em>) – Nb of processes used for multiple emd computation (deprecated)</p></li>
<li><p><strong>numItermax</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=100000</em><em>)</em>) – The maximum number of iterations before stopping the optimization
algorithm if it has not converged.</p></li>
<li><p><strong>log</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns a dictionary containing the cost and dual
variables. Otherwise returns only the optimal transportation cost.</p></li>
<li><p><strong>return_matrix</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns the optimal transportation matrix in the log.</p></li>
<li><p><strong>center_dual</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, centers the dual potential using function
<code class="xref py py-func docutils literal notranslate"><span class="pre">ot.lp.center_ot_dual()</span></code>.</p></li>
<li><p><strong>numThreads</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em> or </em><em>"max"</em><em>, </em><em>optional</em><em> (</em><em>default=1</em><em>)</em>) – Deprecated compatibility parameter. The network simplex solver no
longer uses OpenMP, so this parameter is ignored.</p></li>
<li><p><strong>check_marginals</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, checks that the marginals mass are equal. If False, skips the
check.</p></li>
<li><p><strong>potentials_init</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.14)"><em>tuple</em></a><em> of </em><em>two arrays</em><em> (</em><em>alpha</em><em>, </em><em>beta</em><em>)</em><em>, </em><em>optional</em><em> (</em><em>default=None</em><em>)</em>) – Warmstart dual potentials to accelerate convergence. Should be a tuple
(alpha, beta) where alpha is shape (ns,) and beta is shape (nt,).
These potentials are used to guide initial pivots in the network simplex.
Typically obtained from a previous EMD solve or Sinkhorn approximation.</p></li>
<li><p><strong>note:</strong> (<em>..</em>) – <p>The solver automatically detects sparse format using the backend’s: <code class="xref py py-meth docutils literal notranslate"><span class="pre">issparse()</span></code> method. For sparse inputs:</p>
<ul>
<li><p>Uses a memory-efficient sparse EMD algorithm</p></li>
<li><p>Edges not included are treated as having infinite cost (forbidden)</p></li>
<li><p>Supports scipy.sparse (NumPy), torch.sparse (PyTorch), etc.</p></li>
<li><p>JAX and TensorFlow backends don’t support sparse matrices</p></li>
</ul>
</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>W</strong> (<em>float, array-like</em>) – Optimal transportation loss for the given parameters</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – If input log is true, a dictionary containing the cost, dual
variables (u, v), exit status, and optionally the optimal
transportation matrix (G) if return_matrix is True</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Simple example with obvious solution. The function emd accepts lists and
perform automatic conversion to numpy arrays</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">ot</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span><span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">b</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span><span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">M</span><span class="o">=</span><span class="p">[[</span><span class="mf">0.</span><span class="p">,</span><span class="mf">1.</span><span class="p">],[</span><span class="mf">1.</span><span class="p">,</span><span class="mf">0.</span><span class="p">]]</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">M</span><span class="p">)</span>
<span class="go">0.0</span>
</pre></div>
</div>
<p class="rubric" id="references-emd2">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id10" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
<p>Bonneel, N., Van De Panne, M., Paris, S., & Heidrich, W.
(2011, December). Displacement interpolation using Lagrangian mass
transport. In ACM Transactions on Graphics (TOG) (Vol. 30, No. 6, p.
158). ACM.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="ot.bregman.html#ot.bregman.sinkhorn" title="ot.bregman.sinkhorn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.bregman.sinkhorn</span></code></a></dt><dd><p>Entropic regularized OT</p>
</dd>
<dt><a class="reference internal" href="ot.optim.html#id0" title="ot.optim.cg"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.optim.cg</span></code></a></dt><dd><p>General regularized OT</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd2_1d">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd2_1d</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x_a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x_b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">metric</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'sqeuclidean'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1.0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dense</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/solver_1d.html#emd2_1d"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd2_1d" title="Link to this definition"></a></dt>
<dd><p>Solves the Earth Movers distance problem between 1d measures and returns
the loss</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\gamma = arg\min_\gamma \sum_i \sum_j \gamma_{ij} d(x_a[i], x_b[j])\\s.t. \gamma 1 = a,
\gamma^T 1= b,
\gamma\geq 0\end{aligned}\end{align} \]</div>
<p>where :</p>
<ul class="simple">
<li><p>d is the metric</p></li>
<li><p><span class="math notranslate nohighlight">\(x_a\)</span> and <span class="math notranslate nohighlight">\(x_b\)</span> are the samples</p></li>
<li><p>a and b are the sample weights</p></li>
</ul>
<p>This implementation only supports metrics
of the form <span class="math notranslate nohighlight">\(d(x, y) = |x - y|^p\)</span>.</p>
<p>Uses the algorithm detailed in <a href="#id28"><span class="problematic" id="id11">[1]_</span></a></p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>x_a</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>ns</em><em>,</em><em>) or </em><em>(</em><em>ns</em><em>, </em><em>1</em><em>)</em>) – Source dirac locations (on the real line)</p></li>
<li><p><strong>x_b</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>nt</em><em>,</em><em>) or </em><em>(</em><em>ns</em><em>, </em><em>1</em><em>)</em>) – Target dirac locations (on the real line)</p></li>
<li><p><strong>a</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>ns</em><em>,</em><em>)</em><em>, </em><em>optional</em>) – Source histogram (default is uniform weight)</p></li>
<li><p><strong>b</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>nt</em><em>,</em><em>)</em><em>, </em><em>optional</em>) – Target histogram (default is uniform weight)</p></li>
<li><p><strong>metric</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.14)"><em>str</em></a><em>, </em><em>optional</em><em> (</em><em>default='sqeuclidean'</em><em>)</em>) – Metric to be used. Only works with either of the strings
<cite>‘sqeuclidean’</cite>, <cite>‘minkowski’</cite>, <cite>‘cityblock’</cite>, or <cite>‘euclidean’</cite>.</p></li>
<li><p><strong>p</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em><em> (</em><em>default=1.0</em><em>)</em>) – The p-norm to apply for if metric=’minkowski’</p></li>
<li><p><strong>dense</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, returns <span class="math notranslate nohighlight">\(\gamma\)</span> as a dense ndarray of shape (ns, nt).
Otherwise returns a sparse representation using scipy’s <cite>coo_matrix</cite>
format. Only used if log is set to True. Due to implementation details,
this function runs faster when dense is set to False.</p></li>
<li><p><strong>log</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns a dictionary containing the transportation matrix.
Otherwise returns only the loss.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>loss</strong> (<em>float</em>) – Cost associated to the optimal transportation</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – If input log is True, a dictionary containing the Optimal transportation
matrix for the given parameters</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Simple example with obvious solution. The function emd2_1d accepts lists and
performs automatic conversion to numpy arrays</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">ot</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span> <span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">b</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span> <span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">x_a</span> <span class="o">=</span> <span class="p">[</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">x_b</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd2_1d</span><span class="p">(</span><span class="n">x_a</span><span class="p">,</span> <span class="n">x_b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="go">0.5</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd2_1d</span><span class="p">(</span><span class="n">x_a</span><span class="p">,</span> <span class="n">x_b</span><span class="p">)</span>
<span class="go">0.5</span>
</pre></div>
</div>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id12" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
<p>Peyré, G., & Cuturi, M. (2017). “Computational Optimal
Transport”, 2018.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#ot.lp.emd2" title="ot.lp.emd2"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.emd2</span></code></a></dt><dd><p>EMD for multidimensional distributions</p>
</dd>
<dt><a class="reference internal" href="#ot.lp.emd_1d" title="ot.lp.emd_1d"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.emd_1d</span></code></a></dt><dd><p>EMD for 1d distributions (returns the transportation matrix instead of the cost)</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd2_lazy">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd2_lazy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">X_a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">X_b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">metric</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'sqeuclidean'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numItermax</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100000</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_matrix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">center_dual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_marginals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">potentials_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/_network_simplex.html#emd2_lazy"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd2_lazy" title="Link to this definition"></a></dt>
<dd><p>Solves the Earth Movers distance problem with lazy cost computation and returns the loss</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\min_\gamma \quad \langle \gamma, \mathbf{M}(\mathbf{X}_a, \mathbf{X}_b) \rangle_F\\s.t. \ \gamma \mathbf{1} = \mathbf{a}\\ \gamma^T \mathbf{1} = \mathbf{b}\\ \gamma \geq 0\end{aligned}\end{align} \]</div>
<p>where :</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(\mathbf{M}(\mathbf{X}_a, \mathbf{X}_b)\)</span> is computed on-the-fly from coordinates</p></li>
<li><p><span class="math notranslate nohighlight">\(\mathbf{a}\)</span> and <span class="math notranslate nohighlight">\(\mathbf{b}\)</span> are the sample weights</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function computes distances on-the-fly during the network simplex algorithm,
avoiding the O(ns*nt) memory cost of pre-computing the full cost matrix. Memory usage
is O(ns+nt) instead.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function is backend-compatible and will work on arrays
from all compatible backends. But the algorithm uses the C++ CPU backend
which can lead to copy overhead on GPU arrays.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>X_a</strong> (<em>(</em><em>ns</em><em>, </em><em>dim</em><em>) </em><em>array-like</em><em>, </em><em>float64</em>) – Source sample coordinates</p></li>
<li><p><strong>X_b</strong> (<em>(</em><em>nt</em><em>, </em><em>dim</em><em>) </em><em>array-like</em><em>, </em><em>float64</em>) – Target sample coordinates</p></li>
<li><p><strong>a</strong> (<em>(</em><em>ns</em><em>,</em><em>) </em><em>array-like</em><em>, </em><em>float64</em><em>, </em><em>optional</em>) – Source histogram (uniform weight if None)</p></li>
<li><p><strong>b</strong> (<em>(</em><em>nt</em><em>,</em><em>) </em><em>array-like</em><em>, </em><em>float64</em><em>, </em><em>optional</em>) – Target histogram (uniform weight if None)</p></li>
<li><p><strong>metric</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.14)"><em>str</em></a><em>, </em><em>optional</em><em> (</em><em>default='sqeuclidean'</em><em>)</em>) – <p>Distance metric for cost computation. Options:</p>
<ul>
<li><p>’sqeuclidean’: Squared Euclidean distance</p></li>
<li><p>’euclidean’: Euclidean distance</p></li>
<li><p>’cityblock’: Manhattan/L1 distance</p></li>
</ul>
</p></li>
<li><p><strong>numItermax</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em><em> (</em><em>default=100000</em><em>)</em>) – Maximum number of iterations before stopping if not converged</p></li>
<li><p><strong>log</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns a dictionary containing the cost, dual variables,
and optionally the transport plan (sparse format)</p></li>
<li><p><strong>return_matrix</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns the optimal transportation matrix in the log (sparse format)</p></li>
<li><p><strong>center_dual</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, centers the dual potential using <code class="xref py py-func docutils literal notranslate"><span class="pre">ot.lp.center_ot_dual()</span></code></p></li>
<li><p><strong>check_marginals</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, checks that the marginals mass are equal</p></li>
<li><p><strong>potentials_init</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.14)"><em>tuple</em></a><em> of </em><em>(</em><em>ns</em><em>,</em><em>) </em><em>and</em><em> (</em><em>nt</em><em>,</em><em>) </em><em>arrays</em><em>, </em><em>optional</em>) – Initial dual potentials (u, v) to warmstart the solver. If provided,
the solver starts from these potentials instead of a cold start.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>W</strong> (<em>float</em>) – Optimal transportation loss</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – If input log is True, a dictionary containing:</p>
<ul>
<li><p>cost: the optimal transportation cost</p></li>
<li><p>u, v: dual variables</p></li>
<li><p>warning: solver status message</p></li>
<li><p>result_code: solver return code</p></li>
<li><p>G: (optional) sparse transport plan if return_matrix=True</p></li>
</ul>
</li>
</ul>
</p>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="../all.html#ot.emd2" title="ot.emd2"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.emd2</span></code></a></dt><dd><p>EMD with pre-computed cost matrix</p>
</dd>
<dt><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.emd_c_lazy</span></code></dt><dd><p>Low-level C++ lazy solver</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd_1d">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd_1d</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x_a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x_b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">metric</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'sqeuclidean'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1.0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dense</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">log</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_marginals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/solver_1d.html#emd_1d"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd_1d" title="Link to this definition"></a></dt>
<dd><p>Solves the Earth Movers distance problem between 1d measures and returns
the OT matrix</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\gamma = arg\min_\gamma \sum_i \sum_j \gamma_{ij} d(x_a[i], x_b[j])\\s.t. \gamma 1 = a,
\gamma^T 1= b,
\gamma\geq 0\end{aligned}\end{align} \]</div>
<p>where :</p>
<ul class="simple">
<li><p>d is the metric</p></li>
<li><p><span class="math notranslate nohighlight">\(x_a\)</span> and <span class="math notranslate nohighlight">\(x_b\)</span> are the samples</p></li>
<li><p>a and b are the sample weights</p></li>
</ul>
<p>This implementation only supports metrics
of the form <span class="math notranslate nohighlight">\(d(x, y) = |x - y|^p\)</span>.</p>
<p>Uses the algorithm detailed in <a href="#id29"><span class="problematic" id="id13">[1]_</span></a></p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>x_a</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>ns</em><em>,</em><em>) or </em><em>(</em><em>ns</em><em>, </em><em>1</em><em>)</em>) – Source dirac locations (on the real line)</p></li>
<li><p><strong>x_b</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>nt</em><em>,</em><em>) or </em><em>(</em><em>ns</em><em>, </em><em>1</em><em>)</em>) – Target dirac locations (on the real line)</p></li>
<li><p><strong>a</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>ns</em><em>,</em><em>)</em><em>, </em><em>optional</em>) – Source histogram (default is uniform weight)</p></li>
<li><p><strong>b</strong> (<em>ndarray</em><em> of </em><em>float64</em><em>, </em><em>shape</em><em> (</em><em>nt</em><em>,</em><em>)</em><em>, </em><em>optional</em>) – Target histogram (default is uniform weight)</p></li>
<li><p><strong>metric</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.14)"><em>str</em></a><em>, </em><em>optional</em><em> (</em><em>default='sqeuclidean'</em><em>)</em>) – Metric to be used. Only works with either of the strings
<cite>‘sqeuclidean’</cite>, <cite>‘minkowski’</cite>, <cite>‘cityblock’</cite>, or <cite>‘euclidean’</cite>.</p></li>
<li><p><strong>p</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.14)"><em>float</em></a><em>, </em><em>optional</em><em> (</em><em>default=1.0</em><em>)</em>) – The p-norm to apply for if metric=’minkowski’</p></li>
<li><p><strong>dense</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, returns <span class="math notranslate nohighlight">\(\gamma\)</span> as a dense ndarray of shape (ns, nt).
Otherwise returns a sparse representation using scipy’s <cite>coo_matrix</cite>
format. Due to implementation details, this function runs faster when
<cite>‘sqeuclidean’</cite>, <cite>‘minkowski’</cite>, <cite>‘cityblock’</cite>, or <cite>‘euclidean’</cite> metrics
are used.</p></li>
<li><p><strong>log</strong> (<em>boolean</em><em>, </em><em>optional</em><em> (</em><em>default=False</em><em>)</em>) – If True, returns a dictionary containing the cost.
Otherwise returns only the optimal transportation matrix.</p></li>
<li><p><strong>check_marginals</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em><em> (</em><em>default=True</em><em>)</em>) – If True, checks that the marginals mass are equal. If False, skips the
check.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>gamma</strong> (<em>ndarray, shape (ns, nt)</em>) – Optimal transportation matrix for the given parameters</p></li>
<li><p><strong>log</strong> (<em>dict</em>) – If input log is True, a dictionary containing the cost and the indices
of the non-zero elements of the transportation matrix</p></li>
</ul>
</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Simple example with obvious solution. The function emd_1d accepts lists and
performs automatic conversion to numpy arrays</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">ot</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span> <span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">b</span><span class="o">=</span><span class="p">[</span><span class="mf">.5</span><span class="p">,</span> <span class="mf">.5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">x_a</span> <span class="o">=</span> <span class="p">[</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">x_b</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd_1d</span><span class="p">(</span><span class="n">x_a</span><span class="p">,</span> <span class="n">x_b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="go">array([[0. , 0.5],</span>
<span class="go"> [0.5, 0. ]])</span>
<span class="gp">>>> </span><span class="n">ot</span><span class="o">.</span><span class="n">emd_1d</span><span class="p">(</span><span class="n">x_a</span><span class="p">,</span> <span class="n">x_b</span><span class="p">)</span>
<span class="go">array([[0. , 0.5],</span>
<span class="go"> [0.5, 0. ]])</span>
</pre></div>
</div>
<p class="rubric">References</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id14" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
<p>Peyré, G., & Cuturi, M. (2017). “Computational Optimal
Transport”, 2018.</p>
</aside>
</aside>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#ot.lp.emd" title="ot.lp.emd"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.emd</span></code></a></dt><dd><p>EMD for multidimensional distributions</p>
</dd>
<dt><a class="reference internal" href="#ot.lp.emd2_1d" title="ot.lp.emd2_1d"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ot.lp.emd2_1d</span></code></a></dt><dd><p>EMD for 1d distributions (returns cost instead of the transportation matrix)</p>
</dd>
</dl>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd_1d_dual_backprop">
<span class="sig-prename descclassname"><span class="pre">ot.lp.</span></span><span class="sig-name descname"><span class="pre">emd_1d_dual_backprop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">u_values</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v_values</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">u_weights</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v_weights</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">require_sort</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/ot/lp/solver_1d.html#emd_1d_dual_backprop"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ot.lp.emd_1d_dual_backprop" title="Link to this definition"></a></dt>
<dd><p>Computes the 1 dimensional OT loss between two (batched) empirical
distributions</p>
<div class="math notranslate nohighlight">
\[OT_{loss} = \int_0^1 |cdf_u^{-1}(q) - cdf_v^{-1}(q)|^p dq\]</div>
<p>and returns the dual potentials and the loss, i.e. such that</p>
<div class="math notranslate nohighlight">
\[OT_{loss}(u,v) = \int f(x)\mathrm{d}u(x) + \int g(y)\mathrm{d}v(y).\]</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This function only works in pytorch or jax as it backpropagates through the <cite>wasserstein_1d</cite> function.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>u_values</strong> (<em>array-like</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em>) – locations of the first empirical distribution</p></li>
<li><p><strong>v_values</strong> (<em>array-like</em><em>, </em><em>shape</em><em> (</em><em>m</em><em>, </em><em>...</em><em>)</em>) – locations of the second empirical distribution</p></li>
<li><p><strong>u_weights</strong> (<em>array-like</em><em>, </em><em>shape</em><em> (</em><em>n</em><em>, </em><em>...</em><em>)</em><em>, </em><em>optional</em>) – weights of the first empirical distribution, if None then uniform weights are used</p></li>
<li><p><strong>v_weights</strong> (<em>array-like</em><em>, </em><em>shape</em><em> (</em><em>m</em><em>, </em><em>...</em><em>)</em><em>, </em><em>optional</em>) – weights of the second empirical distribution, if None then uniform weights are used</p></li>
<li><p><strong>p</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.14)"><em>int</em></a><em>, </em><em>optional</em>) – order of the ground metric used, should be at least 1, default is 1</p></li>
<li><p><strong>require_sort</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.14)"><em>bool</em></a><em>, </em><em>optional</em>) – sort the distributions atoms locations, if False we will consider they have been sorted prior to being passed to
the function, default is True</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p><ul class="simple">
<li><p><strong>f</strong> (<em>array-like shape (n, …)</em>) – First dual potential</p></li>
<li><p><strong>g</strong> (<em>array-like shape (m, …)</em>) – Second dual potential</p></li>
<li><p><strong>loss</strong> (<em>float/array-like, shape (…)</em>) – the batched EMD</p></li>
</ul>
</p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ot.lp.emd_1d_sorted">