/*
Copyright (c) 2022 Peter Hristozov / Петър Христозов

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! Operation with 3x3 matrices.

/// Represent matrix with dimension 3x3.
pub mod mat_r {
    use crate::math::vector::vec_p::VecP;
    use crate::math::vector::vec_pv::VecPv;
    use crate::math::vector::vec_pva::VecPva;

    /// Matrix with dimension 3x3 (r-matrix)
    #[derive(Debug)]
    pub struct MatR {
        pub r00: f64,
        pub r01: f64,
        pub r02: f64,
        pub r10: f64,
        pub r11: f64,
        pub r12: f64,
        pub r20: f64,
        pub r21: f64,
        pub r22: f64,
    }
    impl MatR {}

    /// Add two r-matrix.
    /// # Arguments
    /// * `a` first r-matrix
    /// * `b` second r-matrix
    /// # Returns
    /// a + b
    pub fn add(a: &MatR, b: &MatR) -> MatR {
        MatR {
            r00: a.r00 + b.r00,
            r01: a.r01 + b.r01,
            r02: a.r02 + b.r02,
            r10: a.r10 + b.r10,
            r11: a.r11 + b.r11,
            r12: a.r12 + b.r12,
            r20: a.r20 + b.r20,
            r21: a.r21 + b.r21,
            r22: a.r22 + b.r22,
        }
    }

    /// Multiply elements of matrix by scalar
    /// # Arguments
    /// * `s` scalar
    /// * `a` matrix
    /// # Returns
    /// s * a
    pub fn scl(s: f64, a: &MatR) -> MatR {
        MatR {
            r00: s * a.r00,
            r01: s * a.r01,
            r02: s * a.r02,
            r10: s * a.r10,
            r11: s * a.r11,
            r12: s * a.r12,
            r20: s * a.r20,
            r21: s * a.r21,
            r22: s * a.r22,
        }
    }

    /// Multiply two r-matrices.
    /// # Arguments
    /// * `a` first r-matrix
    /// * `b` second r-matrix
    /// # Returns
    /// a * b
    pub fn mul(a: &MatR, b: &MatR) -> MatR {
        MatR {
            r00: a.r00 * b.r00 + a.r01 * b.r10 + a.r02 * b.r20,
            r01: a.r00 * b.r01 + a.r01 * b.r11 + a.r02 * b.r21,
            r02: a.r00 * b.r02 + a.r01 * b.r12 + a.r02 * b.r22,
            r10: a.r10 * b.r00 + a.r11 * b.r10 + a.r12 * b.r20,
            r11: a.r10 * b.r01 + a.r11 * b.r11 + a.r12 * b.r21,
            r12: a.r10 * b.r02 + a.r11 * b.r12 + a.r12 * b.r22,
            r20: a.r20 * b.r00 + a.r21 * b.r10 + a.r22 * b.r20,
            r21: a.r20 * b.r01 + a.r21 * b.r11 + a.r22 * b.r21,
            r22: a.r20 * b.r02 + a.r21 * b.r12 + a.r22 * b.r22,
        }
    }

    /// Transpose an r-matrix.
    /// # Arguments
    /// * `a` r-matrix
    /// # Returns
    /// transpose r-matrix `a`
    pub fn tr(a: &MatR) -> MatR {
        MatR {
            r00: a.r00,
            r01: a.r10,
            r02: a.r20,
            r10: a.r01,
            r11: a.r11,
            r12: a.r21,
            r20: a.r02,
            r21: a.r12,
            r22: a.r22,
        }
    }

    /// Rotation matrix about the x-axis
    /// # Arguments
    /// * `angle` angle in radian
    /// # Returns
    /// ( 1       0             0       )
    /// ( 0  + cos(angle)  + sin(angle) )
    /// ( 0  - sin(angle)  + cos(angle) )
    pub fn rot_x(angle: f64) -> MatR {
        let (s, c) = angle.sin_cos();
        MatR {
            r00: 1.0,
            r01: 0.0,
            r02: 0.0,
            r10: 0.0,
            r11: c,
            r12: s,
            r20: 0.0,
            r21: -s,
            r22: c,
        }
    }

    /// Rotation matrix about the y-axis
    /// # Arguments
    /// * `angle` angle in radian
    /// # Returns
    /// ( + cos(angle)    0     - sin(angle) )
    /// (      0          1          0       )
    /// ( + sin(angle)    0     + cos(angle) )
    pub fn rot_y(angle: f64) -> MatR {
        let (s, c) = angle.sin_cos();
        MatR {
            r00: c,
            r01: 0.0,
            r02: -s,
            r10: 0.0,
            r11: 1.0,
            r12: 0.0,
            r20: s,
            r21: 0.0,
            r22: c,
        }
    }

    /// Rotation matrix about the z-axis
    /// # Arguments
    /// * `angle` angle in radian
    /// # Returns
    /// ( + cos(angle)  + sin(angle)    0 )
    /// ( - sin(angle)  + cos(angle)    0 )
    /// (      0             0          1 )
    pub fn rot_z(angle: f64) -> MatR {
        let (s, c) = angle.sin_cos();
        MatR {
            r00: c,
            r01: s,
            r02: 0.0,
            r10: -s,
            r11: c,
            r12: 0.0,
            r20: 0.0,
            r21: 0.0,
            r22: 1.0,
        }
    }

    /// Multiply a p-vector by an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * `a` p-vector
    /// # Returns
    /// r * p
    pub fn mul_p(r: &MatR, a: &VecP) -> VecP {
        VecP {
            x: r.r00 * a.x + r.r01 * a.y + r.r02 * a.z,
            y: r.r10 * a.x + r.r11 * a.y + r.r12 * a.z,
            z: r.r20 * a.x + r.r21 * a.y + r.r22 * a.z,
        }
    }

    /// Multiply a p-vector by the transpose of an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * `a` p-vector
    /// # Returns
    /// r^T * p
    pub fn multr_p(r: &MatR, a: &VecP) -> VecP {
        VecP {
            x: r.r00 * a.x + r.r10 * a.y + r.r20 * a.z,
            y: r.r01 * a.x + r.r11 * a.y + r.r21 * a.z,
            z: r.r02 * a.x + r.r12 * a.y + r.r22 * a.z,
        }
    }

    /// Multiply a pv-vector by an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * 'a` pv-vector
    /// # Returns
    /// r * pv
    pub fn mul_pv(r: &MatR, a: &VecPv) -> VecPv {
        VecPv {
            x: r.r00 * a.x + r.r01 * a.y + r.r02 * a.z,
            y: r.r10 * a.x + r.r11 * a.y + r.r12 * a.z,
            z: r.r20 * a.x + r.r21 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx + r.r01 * a.vy + r.r02 * a.vz,
            vy: r.r10 * a.vx + r.r11 * a.vy + r.r12 * a.vz,
            vz: r.r20 * a.vx + r.r21 * a.vy + r.r22 * a.vz,
        }
    }

    /// Multiply a pv-vector by the transpose of an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * `a` pv-vector
    /// # Returns
    /// r^T * pv
    pub fn multr_pv(r: &MatR, a: &VecPv) -> VecPv {
        VecPv {
            x: r.r00 * a.x + r.r10 * a.y + r.r20 * a.z,
            y: r.r01 * a.x + r.r11 * a.y + r.r21 * a.z,
            z: r.r02 * a.x + r.r12 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx + r.r10 * a.vy + r.r20 * a.vz,
            vy: r.r01 * a.vx + r.r11 * a.vy + r.r21 * a.vz,
            vz: r.r02 * a.vx + r.r12 * a.vy + r.r22 * a.vz,
        }
    }

    /// Multiply a pva-vector by an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * 'a` pva-vector
    /// # Returns
    /// r * pva
    pub fn mul_pva(r: &MatR, a: &VecPva) -> VecPva {
        VecPva {
            x: r.r00 * a.x + r.r01 * a.y + r.r02 * a.z,
            y: r.r10 * a.x + r.r11 * a.y + r.r12 * a.z,
            z: r.r20 * a.x + r.r21 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx + r.r01 * a.vy + r.r02 * a.vz,
            vy: r.r10 * a.vx + r.r11 * a.vy + r.r12 * a.vz,
            vz: r.r20 * a.vx + r.r21 * a.vy + r.r22 * a.vz,
            ax: r.r00 * a.ax + r.r01 * a.ay + r.r02 * a.az,
            ay: r.r10 * a.ax + r.r11 * a.ay + r.r12 * a.az,
            az: r.r20 * a.ax + r.r21 * a.ay + r.r22 * a.az,
        }
    }

    /// Multiply a pva-vector by the transpose of an r-matrix.
    /// # Arguments
    /// * `r` r-matrix
    /// * `a` pva-vector
    /// # Returns
    /// r^T * pva
    pub fn multr_pva(r: &MatR, a: &VecPva) -> VecPva {
        VecPva {
            x: r.r00 * a.x + r.r10 * a.y + r.r20 * a.z,
            y: r.r01 * a.x + r.r11 * a.y + r.r21 * a.z,
            z: r.r02 * a.x + r.r12 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx + r.r10 * a.vy + r.r20 * a.vz,
            vy: r.r01 * a.vx + r.r11 * a.vy + r.r21 * a.vz,
            vz: r.r02 * a.vx + r.r12 * a.vy + r.r22 * a.vz,
            ax: r.r00 * a.ax + r.r10 * a.ay + r.r20 * a.az,
            ay: r.r01 * a.ax + r.r11 * a.ay + r.r21 * a.az,
            az: r.r02 * a.ax + r.r12 * a.ay + r.r22 * a.az,
        }
    }

    /// Express an r-matrix as an rotation vector.
    /// # Arguments
    /// * `rm` rotation matrix
    /// # Returns
    /// rotation vector
    pub fn rmat_rotvec(rm: &MatR) -> VecP {
        let x = rm.r12 - rm.r21;
        let y = rm.r20 - rm.r02;
        let z = rm.r01 - rm.r10;
        let s2 = (x * x + y * y + z * z).sqrt();
        if s2 > 0.0 {
            let c2 = rm.r00 + rm.r11 + rm.r22 - 1.0;
            let phi = s2.atan2(c2);
            let f = phi / s2;
            VecP {
                x: x * f,
                y: y * f,
                z: z * f,
            }
        } else {
            VecP {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            }
        }
    }

    /// Form the r-matrix corresponding to a given rotation vector.
    /// # Arguments
    /// *`rv` rotation vector, its magnitude is the angle in radians
    /// # Returns
    /// rotation matrix
    pub fn rotvec_rmat(rv: &VecP) -> MatR {
        let mut x = rv.x;
        let mut y = rv.y;
        let mut z = rv.z;
        let phi = rv.modul();
        let (s, c) = phi.sin_cos();
        let f = 1.0 - c;
        if phi > 0.0 {
            x /= phi;
            y /= phi;
            z /= phi;
        }
        MatR {
            r00: (x * x * f + c),
            r01: (x * y * f + z * s),
            r02: (x * z * f - y * s),
            r10: (y * x * f - z * s),
            r11: (y * y * f + c),
            r12: (y * z * f + x * s),
            r20: (z * x * f + y * s),
            r21: (z * y * f - x * s),
            r22: (z * z * f + c),
        }
    }
}

/// Represent matrix with dimension 3x3 as function of time.
pub mod mat_rv {

    use crate::math::scalar::scl_sv::SclSv;
    use crate::math::vector::vec_pv::VecPv;
    use crate::math::vector::vec_pva::VecPva;

    /// Matrix with dimension 3x3 and velocity of his component (rv-matrix).
    #[derive(Debug)]
    pub struct MatRv {
        pub r00: f64,
        pub r01: f64,
        pub r02: f64,
        pub r10: f64,
        pub r11: f64,
        pub r12: f64,
        pub r20: f64,
        pub r21: f64,
        pub r22: f64,
        pub v01: f64,
        pub v00: f64,
        pub v02: f64,
        pub v10: f64,
        pub v11: f64,
        pub v12: f64,
        pub v20: f64,
        pub v21: f64,
        pub v22: f64,
    }
    impl MatRv {}

    pub fn add(a: &MatRv, b: &MatRv) -> MatRv {
        MatRv {
            r00: a.r00 + b.r00,
            r01: a.r01 + b.r01,
            r02: a.r02 + b.r02,
            r10: a.r10 + b.r10,
            r11: a.r11 + b.r11,
            r12: a.r12 + b.r12,
            r20: a.r20 + b.r20,
            r21: a.r21 + b.r21,
            r22: a.r22 + b.r22,
            v00: a.v00 + b.v00,
            v01: a.v01 + b.v01,
            v02: a.v02 + b.v02,
            v10: a.v10 + b.v10,
            v11: a.v11 + b.v11,
            v12: a.v12 + b.v12,
            v20: a.v20 + b.v20,
            v21: a.v21 + b.v21,
            v22: a.v22 + b.v22,
        }
    }

    pub fn scl(s: f64, a: &MatRv) -> MatRv {
        MatRv {
            r00: s * a.r00,
            r01: s * a.r01,
            r02: s * a.r02,
            r10: s * a.r10,
            r11: s * a.r11,
            r12: s * a.r12,
            r20: s * a.r20,
            r21: s * a.r21,
            r22: s * a.r22,
            v00: s * a.v00,
            v01: s * a.v01,
            v02: s * a.v02,
            v10: s * a.v10,
            v11: s * a.v11,
            v12: s * a.v12,
            v20: s * a.v20,
            v21: s * a.v21,
            v22: s * a.v22,
        }
    }

    pub fn mul(a: &MatRv, b: &MatRv) -> MatRv {
        MatRv {
            r00: a.r00 * b.r00 + a.r01 * b.r10 + a.r02 * b.r20,
            r01: a.r00 * b.r01 + a.r01 * b.r11 + a.r02 * b.r21,
            r02: a.r00 * b.r02 + a.r01 * b.r12 + a.r02 * b.r22,
            r10: a.r10 * b.r00 + a.r11 * b.r10 + a.r12 * b.r20,
            r11: a.r10 * b.r01 + a.r11 * b.r11 + a.r12 * b.r21,
            r12: a.r10 * b.r02 + a.r11 * b.r12 + a.r12 * b.r22,
            r20: a.r20 * b.r00 + a.r21 * b.r10 + a.r22 * b.r20,
            r21: a.r20 * b.r01 + a.r21 * b.r11 + a.r22 * b.r21,
            r22: a.r20 * b.r02 + a.r21 * b.r12 + a.r22 * b.r22,
            v00: a.v00 * b.r00
                + a.v01 * b.r10
                + a.v02 * b.r20
                + a.r00 * b.v00
                + a.r01 * b.v10
                + a.r02 * b.v20,
            v01: a.v00 * b.r01
                + a.v01 * b.r11
                + a.v02 * b.r21
                + a.r00 * b.v01
                + a.r01 * b.v11
                + a.r02 * b.v21,
            v02: a.v00 * b.r02
                + a.v01 * b.r12
                + a.v02 * b.r22
                + a.r00 * b.v02
                + a.r01 * b.v12
                + a.r02 * b.v22,
            v10: a.v10 * b.r00
                + a.v11 * b.r10
                + a.v12 * b.r20
                + a.r10 * b.v00
                + a.r11 * b.v10
                + a.r12 * b.v20,
            v11: a.v10 * b.r01
                + a.v11 * b.r11
                + a.v12 * b.r21
                + a.r10 * b.v01
                + a.r11 * b.v11
                + a.r12 * b.v21,
            v12: a.v10 * b.r02
                + a.v11 * b.r12
                + a.v12 * b.r22
                + a.r10 * b.v02
                + a.r11 * b.v12
                + a.r12 * b.v22,
            v20: a.v20 * b.r00
                + a.v21 * b.r10
                + a.v22 * b.r20
                + a.r20 * b.v00
                + a.r21 * b.v10
                + a.r22 * b.v20,
            v21: a.v20 * b.r01
                + a.v21 * b.r11
                + a.v22 * b.r21
                + a.r20 * b.v01
                + a.r21 * b.v11
                + a.r22 * b.v21,
            v22: a.v20 * b.r02
                + a.v21 * b.r12
                + a.v22 * b.r22
                + a.r20 * b.v02
                + a.r21 * b.v12
                + a.r22 * b.v22,
        }
    }

    pub fn tr(a: &MatRv) -> MatRv {
        MatRv {
            r00: a.r00,
            r01: a.r10,
            r02: a.r20,
            r10: a.r01,
            r11: a.r11,
            r12: a.r21,
            r20: a.r02,
            r21: a.r12,
            r22: a.r22,
            v00: a.v00,
            v01: a.v10,
            v02: a.v20,
            v10: a.v01,
            v11: a.v11,
            v12: a.v21,
            v20: a.v02,
            v21: a.v12,
            v22: a.v22,
        }
    }

    pub fn rot_x(angle: &SclSv) -> MatRv {
        let (s, c) = angle.s.sin_cos();
        MatRv {
            r00: 1.0,
            r01: 0.0,
            r02: 0.0,
            r10: 0.0,
            r11: c,
            r12: s,
            r20: 0.0,
            r21: -s,
            r22: c,
            v00: 0.0,
            v01: 0.0,
            v02: 0.0,
            v10: 0.0,
            v11: -s * angle.v,
            v12: c * angle.v,
            v20: 0.0,
            v21: -c * angle.v,
            v22: -s * angle.v,
        }
    }

    pub fn rot_y(angle: &SclSv) -> MatRv {
        let (s, c) = angle.s.sin_cos();
        MatRv {
            r00: c,
            r01: 0.0,
            r02: -s,
            r10: 0.0,
            r11: 1.0,
            r12: 0.0,
            r20: s,
            r21: 0.0,
            r22: c,
            v00: -s * angle.v,
            v01: 0.0,
            v02: -c * angle.v,
            v10: 0.0,
            v11: 0.0,
            v12: 0.0,
            v20: c * angle.v,
            v21: 0.0,
            v22: -s * angle.v,
        }
    }

    pub fn rot_z(angle: &SclSv) -> MatRv {
        let (s, c) = angle.s.sin_cos();
        MatRv {
            r00: c,
            r01: s,
            r02: 0.0,
            r10: -s,
            r11: c,
            r12: 0.0,
            r20: 0.0,
            r21: 0.0,
            r22: 1.0,
            v00: -s * angle.v,
            v01: c * angle.v,
            v02: 0.0,
            v10: -c * angle.v,
            v11: -s * angle.v,
            v12: 0.0,
            v20: 0.0,
            v21: 0.0,
            v22: 0.0,
        }
    }

    pub fn mul_pv(r: &MatRv, a: &VecPv) -> VecPv {
        VecPv {
            x: r.r00 * a.x + r.r01 * a.y + r.r02 * a.z,
            y: r.r10 * a.x + r.r11 * a.y + r.r12 * a.z,
            z: r.r20 * a.x + r.r21 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx
                + r.r01 * a.vy
                + r.r02 * a.vz
                + r.v00 * a.x
                + r.v01 * a.y
                + r.v02 * a.z,
            vy: r.r10 * a.vx
                + r.r11 * a.vy
                + r.r12 * a.vz
                + r.v10 * a.x
                + r.v11 * a.y
                + r.v12 * a.z,
            vz: r.r20 * a.vx
                + r.r21 * a.vy
                + r.r22 * a.vz
                + r.v20 * a.x
                + r.v21 * a.y
                + r.v22 * a.z,
        }
    }

    pub fn multr_pv(r: &MatRv, a: &VecPv) -> VecPv {
        VecPv {
            x: r.r00 * a.x + r.r10 * a.y + r.r20 * a.z,
            y: r.r01 * a.x + r.r11 * a.y + r.r21 * a.z,
            z: r.r02 * a.x + r.r12 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx
                + r.r10 * a.vy
                + r.r20 * a.vz
                + r.v00 * a.x
                + r.v10 * a.y
                + r.v20 * a.z,
            vy: r.r01 * a.vx
                + r.r11 * a.vy
                + r.r21 * a.vz
                + r.v01 * a.x
                + r.v11 * a.y
                + r.v21 * a.z,
            vz: r.r02 * a.vx
                + r.r12 * a.vy
                + r.r22 * a.vz
                + r.v02 * a.x
                + r.v12 * a.y
                + r.v22 * a.z,
        }
    }

    pub fn mul_pva(r: &MatRv, a: &VecPva) -> VecPva {
        VecPva {
            x: r.r00 * a.x + r.r01 * a.y + r.r02 * a.z,
            y: r.r10 * a.x + r.r11 * a.y + r.r12 * a.z,
            z: r.r20 * a.x + r.r21 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx
                + r.r01 * a.vy
                + r.r02 * a.vz
                + r.v00 * a.x
                + r.v01 * a.y
                + r.v02 * a.z,
            vy: r.r10 * a.vx
                + r.r11 * a.vy
                + r.r12 * a.vz
                + r.v10 * a.x
                + r.v11 * a.y
                + r.v12 * a.z,
            vz: r.r20 * a.vx
                + r.r21 * a.vy
                + r.r22 * a.vz
                + r.v20 * a.x
                + r.v21 * a.y
                + r.v22 * a.z,
            ax: r.r00 * a.ax
                + 2.0 * r.v00 * a.vx
                + r.r01 * a.ay
                + 2.0 * r.v01 * a.vy
                + r.r02 * a.az
                + 2.0 * r.v02 * a.vz,
            ay: r.r10 * a.ax
                + 2.0 * r.v10 * a.vx
                + r.r11 * a.ay
                + 2.0 * r.v11 * a.vy
                + r.r12 * a.az
                + 2.0 * r.v12 * a.vz,
            az: r.r20 * a.ax
                + 2.0 * r.v20 * a.vx
                + r.r21 * a.ay
                + 2.0 * r.v21 * a.vy
                + r.r22 * a.az
                + 2.0 * r.v22 * a.vz,
        }
    }

    pub fn multr_pva(r: &MatRv, a: &VecPva) -> VecPva {
        VecPva {
            x: r.r00 * a.x + r.r10 * a.y + r.r20 * a.z,
            y: r.r01 * a.x + r.r11 * a.y + r.r21 * a.z,
            z: r.r02 * a.x + r.r12 * a.y + r.r22 * a.z,
            vx: r.r00 * a.vx
                + r.r10 * a.vy
                + r.r20 * a.vz
                + r.v00 * a.x
                + r.v10 * a.y
                + r.v20 * a.z,
            vy: r.r01 * a.vx
                + r.r11 * a.vy
                + r.r21 * a.vz
                + r.v01 * a.x
                + r.v11 * a.y
                + r.v21 * a.z,
            vz: r.r02 * a.vx
                + r.r12 * a.vy
                + r.r22 * a.vz
                + r.v02 * a.x
                + r.v12 * a.y
                + r.v22 * a.z,
            ax: r.r00 * a.ax
                + 2.0 * r.v00 * a.vx
                + r.r10 * a.ay
                + 2.0 * r.v10 * a.vy
                + r.r20 * a.az
                + 2.0 * r.v20 * a.vz,
            ay: r.r01 * a.ax
                + 2.0 * r.v01 * a.vx
                + r.r11 * a.ay
                + 2.0 * r.v11 * a.vy
                + r.r21 * a.az
                + 2.0 * r.v21 * a.vz,
            az: r.r02 * a.ax
                + 2.0 * r.v02 * a.vx
                + r.r12 * a.ay
                + 2.0 * r.v12 * a.vy
                + r.r22 * a.az
                + 2.0 * r.v22 * a.vz,
        }
    }
}
