Constrained tensor product in F#

Let’s define a tensor product of two different types

scalar-vector-matrix-tensor

in quantum mechanics there is this ket bra product also known as outer product

\left|\Psi\right> \left<\Phi\right|

in F# we can write a function

 let tensor xvect yvect =
    xvect
    |> Seq.map  (fun x ->
        yvect
        |> Seq.map  (fun y -> x, y))
        |> Seq.concat

Now we want to introduce a contraint such that each left or right vector appears only one time

Basically we have to

  1.  fold through the tensor
  2. check if a vector already exists in the accumulator
  3. if it is not the case, we append it into a new state

Now the problem is that we want also to do a compactification to reduce the unbalanced left and right matches to a minimum

Here it is our result

 let cocycle chain =
    chain
    |> Seq.fold
        (fun cochain cycle->
            let (x: 'x, y: 'y) = cycle
            let foundx =
                cochain
                |> Seq.exists (fun (x1: 'x option,y1: 'y option) -> (x1 = Some x) && (y1.IsSome) )
            let foundy =
                cochain
                |> Seq.exists (fun (x1,y1) -> y1 = Some y && (x1.IsSome))
            match foundx, foundy with
            | true, true ->
                cochain
            |false, false ->
                Seq.append
                    (cochain |> Seq.except [(Some x, None); (None, Some y)] )
                    [(Some x, Some y)]
            |false, true ->
                Seq.append
                    (cochain |> Seq.except [(Some x, None)] )
                    [(Some x, None)]
            |true, false ->
                Seq.append
                    (cochain |> Seq.except [(None, Some y)] )
                    [(None, Some y)]
        )
        Seq.empty

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s