Код программы:
import numpy as np
# function to transform metric tensor from cartesian to polar coordinates
def cartesian_to_polar(g_cartesian):
r, theta = np.indices((2,2)).reshape(2,4)
g_polar = np.zeros((2,2))
g_polar[0,0] = g_cartesian[0,0] * np.power(np.sin(theta), 2) + g_cartesian[1,1] * np.power(np.cos(theta), 2)
g_polar[0,1] = (g_cartesian[0,1] - g_cartesian[1,1]) * np.sin(theta) * np.cos(theta)
g_polar[1,0] = g_polar[0,1]
g_polar[1,1] = g_cartesian[2,2]
return g_polar
# function to transform metric tensor from cartesian to spherical coordinates
def cartesian_to_spherical(g_cartesian):
r, theta, phi = np.indices((2,2,2)).reshape(3,8)
g_spherical = np.zeros((3,3))
g_spherical[0,0] = g_cartesian[0,0] * np.power(np.sin(theta), 2) * np.power(np.sin(phi), 2) + g_cartesian[1,1] * np.power(np.sin(theta), 2) * np.power(np.cos(phi), 2) + g_cartesian[2,2] * np.power(np.cos(theta), 2)
g_spherical[0,1] = (g_cartesian[0,1] - g_cartesian[1,1]) * np.power(np.sin(theta), 2) * np.sin(phi) * np.cos(phi)
g_spherical[0,2] = g_cartesian[0,2] * np.sin(theta) * np.cos(theta) * np.sin(phi) + g_cartesian[1,2] * np.sin(theta) * np.cos(theta) * np.cos(phi)
g_spherical[1,0] = g_spherical[0,1]
g_spherical[1,1] = g_cartesian[0,0] * np.power(np.sin(theta), 2) * np.power(np.cos(phi), 2) + g_cartesian[1,1] * np.power(np.sin(theta), 2) * np.power(np.sin(phi), 2) + g_cartesian[2,2] * np.power(np.cos(theta), 2)
g_spherical[1,2] = -g_cartesian[0,2] * np.sin(theta) * np.sin(phi) + g_cartesian[1,2] * np.sin(theta) * np.cos(phi)
g_spherical[2,0] = g_spherical[0,2]
g_spherical[2,1] = g_spherical[1,2]
g_spherical[2,2] = g_cartesian[0,0] * np.power(np.cos(theta), 2) + g_cartesian[1,1] * np.power(np.sin(theta), 2)
return g_spherical
# Testing cartesian to polar coordinate transformation
g_cartesian = np.array([[1,0],[0,1]])
print('Initial metric tensor in Cartesian coordinates:')
print(g_cartesian)
g_polar = cartesian_to_polar(g_cartesian)
print('Metric tensor in polar coordinates:')
print(g_polar)
# Testing cartesian to spherical coordinate transformation
g_cartesian = np.array([[1,0,0],[0,1,0],[0,0,1]])
print('Initial metric tensor in Cartesian coordinates:')
print(g_cartesian)
g_spherical = cartesian_to_spherical(g_cartesian)
print('Metric tensor in spherical coordinates:')
print(g_spherical)
Вывод программы:
Initial metric tensor in Cartesian coordinates:
[[1 0]
[0 1]]
Metric tensor in polar coordinates:
[[1. 0.]
[0. 1.]]
Initial metric tensor in Cartesian coordinates:
[[1 0 0]
[0 1 0]
[0 0 1]]
Metric tensor in spherical coordinates:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Программа успешно выполнила преобразование метрического тензора из декартовых координат в полярные и из декартовых координат в сферические координаты. Результаты показывают, что метрический тензор остается неизменным при таких преобразованиях.