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
use self::ndarray_linalg::{Eigh, UPLO};
use crate::algebra::*;
use crate::expr::Names;
use crate::flag::Flag;
use crate::operator::{Basis, Savable};
use crate::sdp::*;
use crate::tools::Draw;
use ndarray::ScalarOperand;
use num::{FromPrimitive, Num, Zero};

use sprs::CsMat;
use std::fmt::Display;
use std::fs::File;
use std::io::*;
use std::ops::AddAssign;
use std::path::PathBuf;
extern crate ndarray_linalg;
use ndarray::Array1;
use ndarray::Array2;

/// Describe element in an html page
pub trait Html {
    fn print_html<W: Write>(&self, w: &mut W) -> Result<()>;

    const LATEX: bool = false;
    /// True if Mathjax need to be loaded

    fn html(&self, name: &str) -> Result<()> {
        let mut filename = PathBuf::from(name);
        let _ = filename.set_extension("html");
        let mut file = BufWriter::new(File::create(&filename)?);
        writeln!(file, "<!DOCTYPE html><html><head><title>{}</title>", name)?;
        if Self::LATEX {
            writeln!(file, "{}", MATHJAX)?;
        }
        writeln!(file, "<style>{}</style></head><body>", CSS)?;
        self.print_html(&mut file)?;
        writeln!(file, "</body>")
    }
}

impl<F: Draw> Html for F {
    fn print_html<W: Write>(&self, w: &mut W) -> Result<()> {
        writeln!(w, "{}", self.draw())
    }
}

const CSS: &str = "
:root {
    --color1: #a3bbdc;
    --color2: #dae4f1;
    --color3: #edf2f8;
    --color4: #ffe8d6;
    --darkcolor4: #ff971d;
}
h2 {
    color: var(--darkcolor4);
}
body {
    background-color: #133454;
}
.flags {
    background-color: var(--color3);
    margin: 5px 10px;
}
details[open] > summary {
    background-color: var(--color1);
}
details {
    background-color: var(--color2);
}
details[open] {
    padding-bottom: 5px;
    margin-bottom: 5px;
}
div.qflag_item {
    display: inline-block;
    margin: 10px;
    text-align: center;
}
.qflag_item > span {
    display: block;
}
div.inequality {
    border-left-style:solid;
    border-color: var(--darkcolor4);
    border-width: thick;
    background-color: var(--color4);
    margin: 10px 0px 10px;
    padding: 10px;
}
div.obj {
    border-radius: 25px;
    background-color: var(--color4);
    margin: 10px 0px 10px;
    padding: 10px;

}
svg.inline-flag {
    width: 60px;
    top: 20px;
    position: relative;
}
";

const MATHJAX: &str = "<script>
MathJax = {
  tex: {
    inlineMath: ['\\\\[', '\\\\]', ['$','$']],
  }
};
</script>
     <script type=\"text/javascript\" id=\"MathJax-script\" async
     src=\"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js\">
     </script>";

fn print_tab<W, F, G, L>(w: &mut W, tab: &[G], mut f: F, type_size: usize) -> Result<()>
where
    W: Write,
    F: FnMut(usize, &G) -> Option<L>,
    G: Draw,
    L: Display,
{
    writeln!(w, "<div class=\"flags\">")?;
    for (i, x) in tab.iter().enumerate() {
        if let Some(label) = f(i, x) {
            writeln!(w, "<div class=\"qflag_item\">")?;
            writeln!(
                w,
                "<span>{}</span>",
                x.draw_with_parameters(|_| 0, type_size)
            )?;
            writeln!(w, "<span>{}</span></div>", label)?;
        }
    }
    writeln!(w, "</div>")
}

impl<G: Draw, F, L> Html for (&[G], F)
where
    F: FnMut(&G) -> L + Clone,
    L: Display,
{
    const LATEX: bool = G::LATEX;

    fn print_html<W: Write>(&self, w: &mut W) -> Result<()> {
        let mut f = self.1.clone();
        print_tab(w, self.0, |_, x| Some(f(x)), 0)?;
        Ok(())
    }
}

impl<F> Html for Basis<F>
where
    F: Flag + Draw,
{
    const LATEX: bool = F::LATEX;

    fn print_html<W: Write>(&self, w: &mut W) -> Result<()> {
        print_tab(w, &self.get(), |i, _| Some(i), self.t.size)
    }
}

impl<F, N> Html for QFlag<N, F>
where
    F: Flag + Draw,
    N: Num + Clone + Display + FromPrimitive,
{
    const LATEX: bool = F::LATEX;

    fn print_html<W: Write>(&self, w: &mut W) -> Result<()> {
        let scale: N = N::from_u64(self.scale).unwrap();
        print_tab(
            w,
            &self.basis.get(),
            |i, _| {
                let val = self.data[i].clone();
                if val.is_zero() {
                    None
                } else {
                    Some(val / scale.clone())
                }
            },
            self.basis.t.size,
        )
    }
}

fn inlined_list<'a, I>(iter: I) -> String
where
    I: Iterator + Clone + 'a,
    I::Item: AsRef<str>,
{
    let mut res = String::new();
    let last = iter.clone().count() - 1;
    for (i, word) in iter.enumerate() {
        if i > 0 {
            if i == last {
                res += " and ";
            } else {
                res += ", ";
            }
        };
        res += word.as_ref();
    }
    res
}

impl<N, F> Html for Names<N, F>
where
    F: Flag + Draw,
    N: Num + Clone + Display + FromPrimitive,
{
    const LATEX: bool = F::LATEX;

    fn print_html<W: Write>(&self, w: &mut W) -> Result<()> {
        // Inlined defintions
        let mut inline_names = Vec::new();
        for (t, name) in self.types.iter() {
            let svg = Basis::<F>::new(t.size).get()[t.id].draw_typed(t.size);
            inline_names.push((name, svg))
        }
        for ((i, basis), name) in self.flags.iter() {
            let svg = basis.get()[*i].draw_typed(basis.t.size);
            inline_names.push((name, svg))
        }
        if !inline_names.is_empty() {
            let defs: Vec<_> = inline_names
                .into_iter()
                .map(|(name, svg)| format!("${}=$ {}", name, svg.set("class", "inline-flag")))
                .collect();
            writeln!(w, "<p>where {}.</p>", inlined_list(defs.iter()))?
        }
        // Long definitions
        let other_names: Vec<_> = self
            .sets
            .iter()
            .map(|(name, _, _)| name)
            .chain(self.functions.iter().map(|(name, _)| name))
            .map(|s| format!("${}$", s))
            .collect();
        if !other_names.is_empty() {
            writeln!(
                w,
                "<details><summary>See the definition of {}.</summary>",
                inlined_list(other_names.iter())
            )?;
            // Detailed content
            for (name, basis, flags) in &self.sets {
                writeln!(w, "<p>${}$ contains the following flags:</p>", name)?;
                writeln!(w, "<div class=\"flags\">")?;
                for flag in flags.iter() {
                    writeln!(w, "{}", flag.draw_typed(basis.t.size))?;
                }
                writeln!(w, "</div>")?;
            }
            for (name, qflag) in &self.functions {
                writeln!(w, "<p>${}$ takes the following values:</p>", name)?;
                qflag.print_html(w)?;
            }

            writeln!(w, "</details>")?;
        }
        Ok(())
    }
}

pub trait Approx: Clone + Zero {
    fn is_negligible(&self) -> bool;
    fn round(&self) -> Self {
        if self.is_negligible() {
            Self::zero()
        } else {
            self.clone()
        }
    }
}

impl Approx for f64 {
    fn is_negligible(&self) -> bool {
        self.abs() < 1e-8
    }
}

fn round<N, F: Flag>(vec: &QFlag<N, F>) -> QFlag<N, F>
where
    N: Approx + Zero + Clone,
{
    vec.map(N::round)
}

// <script type=text/x-mathjax-config> MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}}); </script>

fn header<W: Write>(w: &mut W, title: &str) -> Result<()> {
    writeln!(
        w,
        "<!DOCTYPE html><html><head>
<meta charset=\"UTF-8\">
<title>{}</title>
{}
<style>
{}
</style></head><body>",
        title, MATHJAX, CSS,
    )
}

fn footer<W: Write>(w: &mut W) -> Result<()> {
    writeln!(w, "</body></html>")
}

pub fn print_report<N, F>(
    // /!\ select must be applied to pb only
    pb: &ProblemView<N, F>,
    cert: &Certificate<f64>,
    filename: &str,
) -> Result<()>
where
    F: Flag + Draw,
    N: Display + Num + Clone + FromPrimitive + AddAssign + ScalarOperand + From<f64> + Approx,
{
    let mut filename = PathBuf::from(filename);
    let _ = filename.set_extension("html");
    let mut w = BufWriter::new(File::create(&filename)?);
    // Header
    header(&mut w, "Report")?;
    // Objective
    writeln!(w, "<h2>Objective:</h2>")?;
    writeln!(w, "<div class=\"obj\">")?;
    writeln!(w, "<p>Minimize: {}</p>", pb.obj.expr)?;
    {
        let mut names = Names::new();
        writeln!(w, "<p>\\[{}\\]</p>", pb.obj.expr.latex(&mut names))?;
        if !names.is_empty() {
            names.print_html(&mut w)?;
        }
    }
    writeln!(
        w,
        "<details><summary>Flag expression of the objective.</summary><div>"
    )?;
    round(pb.obj).print_html(&mut w)?;
    writeln!(w, "</div></details>")?;
    writeln!(w, "</div>")?;

    writeln!(w, "<h2>Inequalities</h2>")?;

    for (block, ineqs) in pb.ineqs.iter().enumerate() {
        assert!(ineqs.len() > 0);
        writeln!(w, "<div class=\"inequality\">")?;
        writeln!(w, "<p>{:.6}</p>", ineqs.meta())?;
        {
            let mut names = Names::new();
            writeln!(w, "<p>\\[{}\\]</p>", ineqs.meta().latex(&mut names))?;
            names.print_html(&mut w)?;
        }
        write_diag_csmatrix(&mut w, &cert.x[block])?;
        writeln!(w, "<details><summary>Contribution.</summary>")?;
        let coeff: Vec<N> = cert
            .diag_coeffs(block, ineqs.len())
            .into_iter()
            .map(|x| x.into())
            .collect();
        let (lhs, bound) = condense(ineqs, &coeff);
        round(&lhs).print_html(&mut w)?;
        writeln!(w, "\n is at least {}.</details>", bound)?;
        writeln!(w, "</div>")?;
    }

    writeln!(w, "<h2>Cauchy-Schwarz</h2>")?;

    for (block, cs) in pb.cs.iter().enumerate() {
        writeln!(w, "<div class=\"inequality\">")?;
        writeln!(w, "<h4>{}</h4><p>", cs)?;
        svg::write(
            &mut w,
            &cs.1.unlabeling.basis.get()[cs.1.unlabeling.flag].draw(),
        )?;
        writeln!(w, "</p>")?;
        let mat = &cert.x[pb.ineqs.len() + block];
        write_csmatrix(&mut w, mat)?;
        //
        writeln!(w, "<details><summary>Eigenvalue decomposition</summary>")?;
        let a: Array2<f64> = mat.to_dense();
        let (eigenvalues, eigenvectors) = a.eigh(UPLO::Lower).unwrap();
        write_array1(&mut w, &eigenvalues)?;
        write_array2(&mut w, &eigenvectors)?;
        writeln!(w, "</details>")?;
        //
        writeln!(w, "<details><summary>Eigenvectors</summary>")?;
        for (lambda, vect) in &eigenvectors_qflags(&cs, &a) {
            if !lambda.is_negligible() {
                writeln!(w, "<h>λ = {}</h><p>", lambda)?;
                round(vect).print_html(&mut w)?;
                writeln!(w, "</p>")?;
            }
        }
        writeln!(w, "</details>")?;
        //
        writeln!(w, "<details><summary>Contribution.</summary>")?;
        round(&condense_cs(&cs, &a)).print_html(&mut w)?;
        writeln!(w, "</details>")?;
        writeln!(w, "</div>")?;
    }
    footer(&mut w)
}

fn write_csmatrix<W: Write, N: Display>(w: &mut W, mat: &CsMat<N>) -> Result<()> {
    writeln!(w, "<math><mfenced><mtable>")?;
    for i in 0..mat.rows() {
        writeln!(w, "<mtr>")?;
        for j in 0..mat.cols() {
            if let Some(x) = mat.get(i, j) {
                writeln!(w, "<mtd>{}</mtd>", x)?;
            } else {
                writeln!(w, "<mtd></mtd>")?;
            }
        }
        writeln!(w, "</mtr>")?;
    }
    writeln!(w, "</mtable></mfenced></math>")
}

fn write_diag_csmatrix<W: Write, N: Display>(w: &mut W, mat: &CsMat<N>) -> Result<()> {
    writeln!(w, "<math><mfenced><mtable><mtr>")?;
    for i in 0..mat.rows() {
        if let Some(x) = mat.get(i, i) {
            writeln!(w, "<mtd>{}</mtd>", x)?;
        } else {
            writeln!(w, "<mtd>0</mtd>")?;
        }
    }
    writeln!(w, "</mtr></mtable></mfenced></math>")
}

fn write_array2<W: Write, N: Display>(w: &mut W, mat: &Array2<N>) -> Result<()> {
    writeln!(w, "<math><mfenced><mtable>")?;
    for i in 0..mat.nrows() {
        writeln!(w, "<mtr>")?;
        for j in 0..mat.ncols() {
            writeln!(w, "<mtd>{}</mtd>", mat[(i, j)])?;
        }
        writeln!(w, "</mtr>")?;
    }
    writeln!(w, "</mtable></mfenced></math>")
}

fn write_array1<W: Write, N: Display>(w: &mut W, mat: &Array1<N>) -> Result<()> {
    writeln!(w, "<math><mfenced><mtable>")?;
    writeln!(w, "<mtr>")?;
    for x in mat {
        writeln!(w, "<mtd>{}</mtd>", x)?;
    }
    writeln!(w, "</mtr>")?;
    writeln!(w, "</mtable></mfenced></math>")
}

// For including quantum flags in latex reports
pub fn latexify<F, N>(qflag: &QFlag<N, F>, folder: &str)
where
    F: Flag + Draw,
    N: Num + Clone + Display + FromPrimitive,
{
    let mut path = PathBuf::from(folder);
    let scale: N = N::from_u64(qflag.scale).unwrap();
    for (i, (val, flag)) in qflag
        .data
        .iter()
        .zip(qflag.basis.get().into_iter())
        .enumerate()
    {
        if !val.is_zero() {
            let b = qflag.basis;
            let filename = format!("{}in{}t{}id{}", i, b.size, b.t.size, b.t.id);
            path.push(&filename);
            let _ = path.set_extension("svg");
            svg::save(&path, &flag.draw_typed(qflag.basis.t.size)).unwrap();
            let x = val.clone() / scale.clone();
            if x.is_one() {
                print!(" + \\flag{{{}}}", filename)
            } else {
                print!(" + {}\\cdot\\flag{{{}}}", val, filename)
            };
            assert!(path.pop());
        }
    }
}