简体   繁体   English

递归求根平方和

[英]Recursively find the root squared sum

I am trying to recursively find the root squared sum of two arrays.我试图递归地找到两个 arrays 的平方和。 Basically given:基本上给出:

array1 = {1,5,8};
array2 = {2,9,10};
RSS = sqrt((1-2)^2 + (2-5)^2 + (3-8)^2) = 4.58258

I have implemented the following code, but after testing I know that it does not work.我已经实现了以下代码,但经过测试我知道它不起作用。 I am returning the square root each time, so my RSS value is lower than it should be.我每次都返回平方根,所以我的 RSS 值低于应有的值。

double findRSS(int* array1, int* array2, int size){
    double sum = 0;
    if (size <= 0){
        return 0;   }
    else{
        sum = pow((array1[size-1] - array2[size-1]), 2);
        sum = sum + findRSS(array1, array2, size-1);
    }
    return sqrt(sum);
}

For the above example I am returning 2.85011 instead.对于上面的示例,我改为返回 2.85011。

I feel I am close but just have a small issue.我觉得我很接近,但只是有一个小问题。 Can someone help me out?有人可以帮我吗?

You are calculating sqrt((1-2)^2 + sqrt((2-5)^2 + sqrt((3-8)^2))) you can do somewhat simple "fix" to your logic by squaring recursed result once您正在计算 sqrt((1-2)^2 + sqrt((2-5)^2 + sqrt((3-8)^2))) 您可以通过平方递归结果对您的逻辑进行一些简单的“修复”一次

double findRSS(int* array1, int* array2, int size){
    double sum = 0;
    if (size <= 0){
        return 0;   }
    else{
        sum = pow((array1[size-1] - array2[size-1]), 2);
        sum = sum + pow(findRSS(array1, array2, size-1), 2); // you are undoing sqrt for new one
    }
    return sqrt(sum);
}

but as you can see the method is flawed.但正如您所见,该方法存在缺陷。 This is a good example of why structured development is important.这是结构化开发为何如此重要的一个很好的例子。 You can find principles developers found that aged well here: http://www.catb.org/~esr/writings/taoup/html/ch01s06.html您可以在这里找到开发人员发现的成熟原理: http://www.catb.org/~esr/writings/taoup/html/ch01s06.html

so your methods would become something like this:所以你的方法会变成这样:

double diffSum(int* array1, int* array2, int size, int power) {
    double sum = 0;
    if (size <= 0) {
        return 0;
    }
    else {
        sum = pow((array1[size - 1] - array2[size - 1]), power);
        sum = sum + diffSum(array1, array2, size - 1, power);
    }
    return sum;
}

double findRSS(int* array1, int* array2, int size) {
    return sqrt(diffSum(array1, array2, size, 2));
}

key to success is when to know you need to separate;成功的关键是何时知道你需要分开; beware keeping methods with fewer lines helps on maintaining the code当心保留较少行的方法有助于维护代码

This does what you want:这可以满足您的要求:

double findRSS(int* array1, int* array2, int size){
    double sum = 0;
    if (size <= 0){
        return 0;   }
    else{
        sum = pow((array1[size-1] - array2[size-1]), 2);
        sum = sum + pow(findRSS(array1, array2, size-1), 2);
    }
    return sqrt(sum);
}

Write out the math by hand to verify that it is correct.用手写出数学来验证它是否正确。

But please don't write actual programs this way.但请不要以这种方式编写实际程序。 Not only is this a needlessly convoluted way to write a trivial loop, it is also horribly inefficient to take a square root at every iteration/recursion just to immediately undo it at the next.这不仅是编写琐碎循环的一种不必要的复杂方式,而且在每次迭代/递归时取平方根以在下一次立即撤消它也是非常低效的。 Even for the simple version below, the single square root at the end will take longer than everything else together:即使对于下面的简单版本,最后的单个平方根也将比其他所有内容花费更长的时间:

double findRSS(int* array1, int* array2)
{
  double sum = 0;
  for (int i = 0; i < size; ++i)
    sum += std::pow(arra1[i] - array2[i], 2);
  return std::sqrt(sum);
}

I mean this way:我的意思是这样:

int findRSS(int *arr1, int *arr2, int size)
{
    if (size == 0)
        return 0;
    return (int)pow(arr1[size - 1] - arr2[size - 1], 2) + findRSS(arr1, arr2, size - 1);
}

int main()
{
    int arr1[] = { 1, 5, 8 };
    int arr2[] = { 2, 9, 10 };
    cout << sqrt(findRSS(arr1, arr2, 3)) << endl;
}

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

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