Visualize 3D Matrix system manipulated by Linear Algebra eqn
Using cross product in this context allows us to better visualize the structure and properties of a nano sample in three dimensions, giving us a clearer understanding of its shape, orientation, and relationships between its various components. This can be especially important for complex or intricate nano samples, where a clear 3D representation can be critical for analysis and interpretation. Run down below the code, add your own data model in 2nd code file.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def cross_product(a, b):
"""
Calculate the cross product of two vectors a and b in 3D space.
Parameters:
a (np.array): first vector
b (np.array): second vector
Returns:
np.array: cross product of vectors a and b
"""
c = np.array([
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]
])
return c
# Example usage
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = cross_product(a, b)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(0, 0, 0, a[0], a[1], a[2], color='r', label='a')
ax.quiver(0, 0, 0, b[0], b[1], b[2], color='g', label='b')
ax.quiver(0, 0, 0, c[0], c[1], c[2], color='b', label='c = a x b')
ax.legend()
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
Cross product running in webrowser.
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/transcrypt@3.7.6/transcrypt.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.0.0/dist/tf.min.js"></script>
<script type="text/javascript">
var __pragma__ = function() {}
var cross_product = function (a, b) {
c = [0, 0, 0];
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
return c;
};
</script>
</head>
<body>
<div id="3d-graph"></div>
<script type="text/javascript">
var a = [1, 2, 3];
var b = [4, 5, 6];
var c = cross_product(a, b);
var x = [0, a[0], b[0], c[0]];
var y = [0, a[1], b[1], c[1]];
var z = [0, a[2], b[2], c[2]];
var trace1 = {
x: x, y: y, z: z,
mode: 'lines',
line: {color: 'red', width: 2},
type: 'scatter3d',
name: 'a'
};
var trace2 = {
x: x, y: y, z: z,
mode: 'lines',
line: {color: 'green', width: 2},
type: 'scatter3d',
name: 'b'
};
var trace3 = {
x: x, y: y, z: z,
mode: 'lines',
line: {color: 'blue', width: 2},
type: 'scatter3d',
name: 'c = a x b'
};
var data = [trace1, trace2, trace3];
var layout = {scene: {xaxis_title: 'X', yaxis_title: 'Y', zaxis_title: 'Z'}};
Plotly.newPlot('3d-graph', data, layout);
</script>
</body>
</html>