• Python:实现lorenz transformation 洛伦兹变换算法(附完整源码)


    Python:实现lorenz transformation 洛伦兹变换算法

    from __future__ import annotations
    
    from math import sqrt
    
    import numpy as np  # type: ignore
    from sympy import symbols  # type: ignore
    
    # Coefficient
    # Speed of light (m/s)
    c = 299792458
    
    # Symbols
    ct, x, y, z = symbols("ct x y z")
    ct_p, x_p, y_p, z_p = symbols("ct' x' y' z'")
    
    
    # Vehicle's speed divided by speed of light (no units)
    def beta(velocity: float) -> float:
        """
        >>> beta(c)
        1.0
    
        >>> beta(199792458)
        0.666435904801848
    
        >>> beta(1e5)
        0.00033356409519815205
    
        >>> beta(0.2)
        Traceback (most recent call last):
          ...
        ValueError: Speed must be greater than 1!
        """
        if velocity > c:
            raise ValueError("Speed must not exceed Light Speed 299,792,458 [m/s]!")
    
        # Usually the speed u should be much higher than 1 (c order of magnitude)
        elif velocity < 1:
            raise ValueError("Speed must be greater than 1!")
        return velocity / c
    
    
    def gamma(velocity: float) -> float:
    
        return 1 / (sqrt(1 - beta(velocity) ** 2))
    
    
    def transformation_matrix(velocity: float) -> np.array:
     
        return np.array(
            [
                [gamma(velocity), -gamma(velocity) * beta(velocity), 0, 0],
                [-gamma(velocity) * beta(velocity), gamma(velocity), 0, 0],
                [0, 0, 1, 0],
                [0, 0, 0, 1],
            ]
        )
    
    
    def transform(
        velocity: float, event: np.array = np.zeros(4), symbolic: bool = True
    ) -> np.array:
    
        # Ensure event is not a vector of zeros
        if not symbolic:
    
            # x0 is ct (speed of ligt * time)
            event[0] = event[0] * c
        else:
    
            # Symbolic four vector
            event = np.array([ct, x, y, z])
    
        return transformation_matrix(velocity).dot(event)
    
    
    if __name__ == "__main__":
        import doctest
    
        doctest.testmod()
    
        # Example of symbolic vector:
        four_vector = transform(29979245)
        print("Example of four vector: ")
        print(f"ct' = {four_vector[0]}")
        print(f"x' = {four_vector[1]}")
        print(f"y' = {four_vector[2]}")
        print(f"z' = {four_vector[3]}")
    
        # Substitute symbols with numerical values:
        values = np.array([1, 1, 1, 1])
        sub_dict = {ct: c * values[0], x: values[1], y: values[2], z: values[3]}
        numerical_vector = [four_vector[i].subs(sub_dict) for i in range(0, 4)]
    
        print(f"\n{numerical_vector}")
    
    
    • 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
  • 相关阅读:
    Python基础入门例程12-NP12 格式化输出(二)
    3.蓝牙模块HC-08
    正版软件|Ashampoo WinOptimizer 26 - Win优化器
    嵌入式Qt-控制硬件:滑动条控制RGB灯
    TCPIP协议学习
    【Unity细节】VS不能附加到Unity程序中解决方法大全
    Workfine新手入门:数据规范之列表
    掌握这些插件,分分钟提高你的办公效率90%!
    php mysql摄影网_图片分享网站
    安全基础~通用漏洞5
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126128865