简体   繁体   English

将 Array2 转换为极坐标

[英]Converting an Array2 to Polar Coordinates

I am struggling to convert an Array2 of Cartesian Coordinates to Polar Coordinates.我正在努力将笛卡尔坐标的 Array2 转换为极坐标。 The two columns of each row have to be mutated based on each others values, and I can't figure out how to make it.每行的两列必须根据彼此的值进行变异,我不知道如何制作。

After different trials, I implemented the following code, but I think it is a dead end:经过不同的尝试,我实现了以下代码,但我认为这是一个死胡同:

use ndarray::{array, Axis};
use num_traits::float::Float;

fn main() {
    let mut coords = array![[1., 0.], [1. / 2., (3.).sqrt() / 2.0], [0., 1.]];


    for mut row in coords.axis_iter_mut(Axis(0)) {
        let col_0 = row.get(0).unwrap();
        let col_1 = row.get(1).unwrap();

        let mut row_polar_coord = array![
            (col_0.powi(2) + col_1.powi(2)).sqrt(),
            (col_1 / col_0).atan()
        ];
        //row = row_polar_coord;
        // Error: mismatched types
        //   expected struct `ndarray::ArrayBase<ndarray::ViewRepr<&mut {float}>, _>`
        //   found struct `ndarray::ArrayBase<ndarray::OwnedRepr<{float}>, _>`
        row = row_polar_coord.view_mut();
        // Diagnostics:
        // `row_polar_coord` does not live long enough
        // borrowed value does not live long enough
    }

}

How should be handled these kinds of transformations in ndarray?在ndarray中应该如何处理这些类型的转换?

I would suggest not using an ndarray to store the real and imaginary part of your values but a tuple.我建议不要使用ndarray来存储值的实部和虚部,而是使用元组。 This has way less overhead.这有更少的开销。

For inplace mutation you can use map_inplace() :对于就地突变,您可以使用map_inplace()

use ndarray::{array, Axis};

fn main() {
    let mut coords = array![(1_f32, 0_f32), (1. / 2., (3_f32).sqrt() / 2.0), (0., 1.)];

    coords.map_inplace(|(a,b)| {
        let c = (a.powi(2) + b.powi(2)).sqrt();
        let d =  (*b / *a).atan();
        *a = c;
        *b = d;
    });
    
    print!("{:?}", coords);

}

When you really need to stick to this input you can do当你真的需要坚持这个输入时,你可以做

use ndarray::{array, Axis};

fn main() {
    let mut coords = array![[1_f32, 0_f32], [1. / 2., (3_f32).sqrt() / 2.0], [0., 1.]];

    coords.axis_iter_mut(Axis(0)).for_each(|mut x| {
        let a : f32 = *x.get(0).unwrap();
        let b : f32 = *x.get(1).unwrap();
    
        x[0] = (a.powi(2) + b.powi(2)).sqrt();
        x[1] =  (b / a).atan();
    });
    
    print!("{:?}", coords);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM