Skip to content

Commit 71a1331

Browse files
committed
editing...
1 parent 3dcfad6 commit 71a1331

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

chapters/ndarray.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -566,46 +566,46 @@ There are way more functions contained in the `Ndarray` module than the ones we
566566
In the last part of this chapter, we will briefly introduce the idea of *tensor*.
567567
If you look at some articles online the tensor is often defined as an n-dimensional array.
568568
However, mathematically, there are differences between these two.
569-
In a n-dimension space, a tensor that contains $m$ indices is a mathematical object that obeys certain transformation rules.
569+
In a n-dimension space, a tensor that contains $$m$$ indices is a mathematical object that obeys certain transformation rules.
570570
For example, in a three dimension space, we have a value `A = [0, 1, 2]` that indicate a vector in this space.
571-
We can find each element in this vector by a single index $i$, e.g. $A_1 = 1$.
571+
We can find each element in this vector by a single index $$i$$, e.g. $$A_1 = 1$$.
572572
This vector is an object in this space, and it stays the same even if we change the standard cartesian coordinate system into other systems.
573-
But if we do so, then the content in $A$ needs to be updated accordingly.
573+
But if we do so, then the content in $$A$$ needs to be updated accordingly.
574574
Therefore we say that, a tensor can normally be expressed in the form of an ndarray, but it is not an ndarray.
575575
That's why we keep using the term "ndarray" in this chapter and through out the book.
576576

577577
The basic idea about tensor is that, since the object stays the same, if we change the coordinate towards one direction, the component of the vector needs to be changed to another direction.
578-
Considering a single vector $v$ in a coordinate system with basis $e$.
579-
We can change the coordinate base to $\tilde{e}$ with linear transformation: $\tilde{e} = Ae$ where A is a matrix. For any vector in this space using $e$ as base, its content will be transformed as: $\tilde{v} = A^{-1}v$, or we can write it as:
578+
Considering a single vector $$v$$ in a coordinate system with basis $e$.
579+
We can change the coordinate base to $$\tilde{e}$$ with linear transformation: $$\tilde{e} = Ae$$ where A is a matrix. For any vector in this space using $e$ as base, its content will be transformed as: $$\tilde{v} = A^{-1}v$$, or we can write it as:
580580

581581
$$\tilde{v}^i = \sum_j~B_j^i~v^j.$$
582582

583-
Here $B=A^{-1}$.
583+
Here $$B=A^{-1}$$.
584584
We call a vector *contravector* because it changes in the opposite way to the basis.
585585
Note we use the superscript to denote the element in contravectors.
586586

587-
As a comparison, think about a matrix multiplication $\alpha~v$. The $\alpha$ itself forms a different vector space, the basis of which is related to the basis of $v$'s vector space.
588-
It turns out that the direction of change of $\alpha$ is the same as that of $e$. When $v$ uses new $\tilde{e} = Ae$, its component changes in the same way:
587+
As a comparison, think about a matrix multiplication $$\alpha~v$$. The $$\alpha$$ itself forms a different vector space, the basis of which is related to the basis of $$v$$'s vector space.
588+
It turns out that the direction of change of $$\alpha$$ is the same as that of $$e$$. When $$v$$ uses new $$\tilde{e} = Ae$$, its component changes in the same way:
589589

590590
$$\tilde{\alpha}_j = \sum_i~A_j^i~\alpha_i.$$
591591

592592
It is called a *covector*, denoted with subscript.
593-
We can further extend it to matrix. Think about a linear mapping $L$. It can be represented as a matrix so that we can apply it to any vector using matrix dot multiplication.
594-
With the change of the coordinate system, it can be proved that the content of the linear map $L$ itself is updated to:
593+
We can further extend it to matrix. Think about a linear mapping $$L$$. It can be represented as a matrix so that we can apply it to any vector using matrix dot multiplication.
594+
With the change of the coordinate system, it can be proved that the content of the linear map $$L$$ itself is updated to:
595595

596596
$$\tilde{L_j^i} = \sum_{kl}~B_k^i~L_l^k~A_j^l.$$
597597

598-
Again, note we use both superscript and subscript for the linear map $L$, since it contains one covariant component and one contravariant component.
598+
Again, note we use both superscript and subscript for the linear map $$L$$, since it contains one covariant component and one contravariant component.
599599
Further more, we can extend this process and define the tensor.
600600
A tensor $T$ is an object that is invariant under a change of coordinates, and with a change of coordinates its component changes in a special way.
601601
The way is that:
602602

603-
$$\tilde{T_{xyz~\ldots}^{abc~\ldots}} = \sum_{ijk\ldots~rst\ldots}~B_i^aB_j^bB_k^c\ldots~T_{rst~\ldots}^{ijk~\ldots}~A_x^rA_y^sA_z^t\ldots$$ {#eq:ndarray:tensor}
603+
$$\tilde{T_{xyz~\ldots}^{abc~\ldots}} = \sum_{ijk\ldots~rst\ldots}~B_i^aB_j^bB_k^c\ldots~T_{rst~\ldots}^{ijk~\ldots}~A_x^rA_y^sA_z^t\ldots$$
604604

605-
Here the $ijk\ldots$ are indices of the contravariant part of the tensor and the $rst\ldots$ are that of the covariant part.
605+
Here the $ijk\ldots$ are indices of the contravariant part of the tensor and the $$rst\ldots$$ are that of the covariant part.
606606

607607
One of the important operations of tensor is the *tensor contraction*. We are familiar with the matrix multiplication:
608-
$$C_j^i = \sum_{k}A_k^iB_j^k.$$ {#eq:ndarray:matmul}
608+
$$C_j^i = \sum_{k}A_k^iB_j^k.$$
609609
The *contraction* operations extends this process to multiple dimension space.
610610
It sums the products of the two ndarrays' elements over specified axes.
611611
For example, we can perform the matrix multiplication with contraction:
@@ -623,7 +623,7 @@ We can see that the matrix multiplication is a special case of contraction opera
623623
Next, let's extend the two dimension case to multiple dimensions.
624624
Let's say we have two three-dimensional array A and B. We hope to compute the matrix C so that:
625625

626-
$$C_j^i = \sum_{hk}~A_{hk}^i~B_j^{kh}$$ {#eq:ndarray:contract}
626+
$$C_j^i = \sum_{hk}~A_{hk}^i~B_j^{kh}$$
627627

628628
We can use the `contract2` function in the `Ndarray` module. It takes an array of `int * int` tuples to specifies the pair of indices in the two input ndarrays. Here is the code:
629629

@@ -634,7 +634,7 @@ let y = Arr.sequential [|4;3;2|]
634634
let z1 = Arr.contract2 [|(0, 1); (1, 0)|] x y
635635
```
636636

637-
The indices mean that, in the contraction, the 0th dimension of `x` corresponds with the 1st dimension of `y`, an the 1st dimension of `x` corresponds with the 0th dimension of `y`, as shown in [@eq:ndarray:contract].
637+
The indices mean that, in the contraction, the 0th dimension of `x` corresponds with the 1st dimension of `y`, an the 1st dimension of `x` corresponds with the 0th dimension of `y`.
638638
We can verify the result with the naive way of implementation:
639639

640640
```ocaml:contraction
@@ -687,16 +687,15 @@ High-performance implementation of the contraction operation has been a research
687687
Actually, many tensor operations involve summation over particular indices.
688688
Therefore in using tensors in applications such as linear algebra and physics, the *Einstein notation* is used to simplified notations.
689689
It removes the common summation notation, and also, any twice-repeated index in a term is summed up (no index is allowed to occur three times or more in a term).
690-
For example, the matrix multiplication notation $C_{ij} = \sum_{k}A_{ik}B_{kj}$ can be simplified as C = $A_{ik}B_{kj}$.
691-
The [@eq:ndarray:tensor] can also be greatly simplified in this way.
690+
For example, the matrix multiplication notation $$C_{ij} = \sum_{k}A_{ik}B_{kj}$$ can be simplified as C = $$A_{ik}B_{kj}$$.
692691

693692
The tensor calculus is of important use in disciplines such as geometry and physics.
694-
More details about the tensor calculation is beyond the scope of this book. We refer readers to work such as [@dullemond1991introduction] for deeper understanding about this topic.
693+
More details about the tensor calculation is beyond the scope of this book.
695694

696695
## Summary
697696

698697
N-dimensional array is the fundamental data type in Owl, as well as in many other numerical libraries such as NumPy.
699-
This chapter explain in detail the Ndarray module, including its creation, properties, manipulation, serialisation, etc.
698+
This chapter explain in detail the Ndarray module, including its creation, properties, manipulation, serialization, etc.
700699
Besides, we also discuss the subtle difference between tensor and ndarray in this chapter.
701700
This chapter is easy to follow, and can serve as a reference whenever users need a quick check of functions they need.
702701

0 commit comments

Comments
 (0)