函数分解成一系列三角函数_看见与看不见的第4部分三角系列

函数分解成一系列三角函数

This is part 4 of the series The Seen and The Unseen. In part 4, I use the trigonometric series to explain how different decisions taken during implementation affect the seen and the unseen.

这是“所见与看不见 ”系列的 4部分。 在第4部分中,我将使用三角函数系列来说明在实现过程中做出的不同决定如何影响可见和不可见的决策。

The trigonometric functions I will be working on are

我将要处理的三角函数是

Image for post

In part 3 — Exponential function, I worked on four cases of optimizations. I will perform these four optimizations on the trigonometric series as well.

第3部分-指数函数中 ,我研究了四种优化情况。 我还将在三角级数上执行这四个优化。

Image for post

天真的实现 (Naïve Implementation)

  1. Cosine Function

    余弦函数
Image for post

Before writing a naïve implementation I want to see how the series progresses as the no.of terms increase.

在编写朴素的实现之前,我想看看随着术语数量的增加,系列如何发展。

The value of the series is oscillating between positive and negative that’s because of (-1) that’s being multiplied at every iteration. The values in red are the 5ᵗʰ term rounded off till the third decimal, they are not necessarily zero.

该系列的值在正负之间波动,这是因为(-1)在每次迭代中都会相乘。 红色的数值是5ᵗʰ的数值,四舍五入至小数点后三位,它们不一定为零。

I’ll be using the same terminating condition I was using for the exponential function. I will stop the loop once the value of the series stops changing.

我将使用与指数函数相同的终止条件。 一旦序列的值停止更改,我将停止循环。

Instead of finding -1^n, I could just do i = i + 2 and alternate the sign to find the cos value.

除了找到-1 ^ n,我可以做i = i + 2并替换符号以找到cos值。

2. Sine Function

2.正弦函数

Image for post

Similarly, sine can be implemented as:

类似地,正弦可以实现为:

The sine and cosine functions are basically the same, the sine function consists of all the odd powered terms and cosine consists of all the even powered terms. Sine and Cosine can be calculated using one function alone.

正弦和余弦函数基本相同,正弦函数由所有奇数幂项组成,余弦由所有偶数幂项组成。 正弦和余弦可以单独使用一个函数来计算。

Here is a function that calculates both sine and cosine

这是一个同时计算正弦和余弦的函数

Using the indicator i = 0 for cosine and i = 1 for sine

使用指标i = 0表示余弦,i = 1表示正弦

Now I’ll test this function for correctness.

现在,我将测试此功能的正确性。

The average runtime for this function is:

该函数的平均运行时间为:

Image for post

情况1 (Case 1)

Image for post

Just like in part 2 and 3, I will terminate the loop once the error is below 0.01%. To ensure that this terminating condition works for cal_sine_cosine_iterations() I will test it using theSeenAndTheUnseenTest().

就像在第2部分和第3部分中一样,一旦错误低于0.01%,我将终止循环。 为确保此终止条件适用于cal_sine_cosine_iterations(),我将使用theSeenAndTheUnseenTest()对其进行测试

See the runtime warning “invalid value encountered in double_scalars” that's because of division by zero. Values such as 𝑐𝑜𝑠 𝜋/2, 𝑠𝑖𝑛 𝜋 are 0 and when I am calculating error with respect to these values I tend to divide by zero. For such cases, I will keep the old terminating condition. Without the testing function, I wouldn't have understood that cal_sine_cosine_iterations() throws a double scalar error for certain values of x.

请参阅运行时警告“在double_scalars中遇到无效的值” ,这是因为被零除。 诸如𝑐𝑜𝑠/ 2,𝑠𝑖𝑛的值为0,并且当我针对这些值计算误差时,我倾向于除以零。 对于这种情况,我将保留旧的终止条件。 没有测试功能,我不会理解cal_sine_cosine_iterations()对于某些x值会引发双标量误差。

Before taking a look at speed up, I want to see the effect on the no.of iterations in this new function.

在看一下加速之前,我想看看这个新函数对迭代次数的影响。

The no.of iterations has reduced by half, I am expecting a speedup of almost 2x.

迭代次数减少了一半,我希望速度提高近2倍。

A speedup of 2x!

加速2倍!

Image for post

情况二 (Case 2)

Image for post

Instead of finding the power at every iteration, I will multiply x to the previous term.

我没有在每次迭代中求幂,而是将x乘以上一项。

Testing cal_sine_cosine_power() correctness.

测试cal_sine_cosine_power()正确性。

cal_sine_cosine_power() gives an error below 0.01%. Now I will see if there is any change in the runtime of cal_sine_cosine_power().

cal_sine_cosine_power()的误差低于0.01%。 现在,我将看看cal_sine_cosine_power()的运行时是否发生任何变化。

Image for post

This case does not give any significant speedup probably because I am still calculating factorial at every iteration. Let’s take a look at the effect on runtime after I apply similar changes for factorial.

这种情况并没有带来任何明显的提速,可能是因为我还在每次迭代中都计算阶乘。 在对阶乘应用类似的更改后,让我们看一下对运行时的影响。

情况3 (Case 3)

Image for post

Just like case 2: power optimization, I will change the way I am calculating factorial for every term.

就像情况2:功效优化一样,我将更改我计算每个项的阶乘的方式。

Testing for correctness.

测试正确性。

Using plot_time()I will find the runtime and speedup for cal_sine_cosine_factorial().

使用plot_time()我会找到一个运行时和加速cal_sine_cosine_factorial()

Image for post

As seen in Part 3 — Exponential Function, power, and factorial case individually don’t give any significant speedup. Let’s take a look at case 4, where the two are put together.

第3部分-指数函数 ,幂和阶乘的情况所示,它们并没有明显提高速度。 让我们看一下案例4,将两者放在一起。

案例4 (Case 4)

Image for post
Image for post
Calculation of terms in case 4
案例4中的项的计算

Just like in The Seen and The Unseen Part 3, I will try another case of optimization for trigonometric function and check if that gives any speedup.

就像在The Seen and The Unseen Part 3中一样 ,我将尝试三角函数优化的另一种情况,并检查是否可以提速。

Testing the fourth case for correctness.

测试第四种情况的正确性。

This newly optimized function cleared all test cases, therefore I can check for speedup.

这项新优化的功能清除了所有测试用例,因此我可以检查速度。

Image for post

Speedup of 2x!

加速2倍!

Putting the power and factorial optimization together gives a speedup of 2x!

将功率和阶乘优化放在一起可以使速度提高2倍!

案例5 (Case 5)

Image for post

Here is a function for calculation cosine and sine with all optimizations together.

这是用于将余弦和正弦与所有优化一起计算的函数。

I have the iteration optimization along with an if statement to avoid the double scalars error, power and factorial optimization together. I need to check whether this function works fine for any given value.

我将迭代优化与if语句一起使用,以避免双重标量错误,功效和阶乘优化。 我需要检查此功能对于任何给定的值是否都能正常工作。

The final part, checking for speedup.

最后一部分,检查加速。

Image for post

All optimizations put together, give a speedup of 4x.

所有优化组合在一起,可以使速度提高4倍

Isn’t it amazing? By making small changes, the function is now four times faster while maintaining an accuracy of 99.99%. I didn’t get a speedup of 4x in one go. I first observed the relationship between the no.of iterations and the value of x. With the visualizing, I not only understood the relation but the impact it had on performance. Only optimizing the function isn’t enough, I had to ensure that the function gave the right outputs for all test cases, I had to ensure the correctness of the function. Using the testing function I understood that there was something wrong with my iteration optimization. I had written generic functions to find the runtime, speed up, and to compare iterations. As discussed in The Seen and The Unseen Part 1, good software is maintainable, ensures correctness, and is reliable. This is the unseen part. By testing, I ensured correctness, writing a generic code ensured maintainability. Visualizing helped in understanding these unseen aspects which helped in making the seen better.

太神奇了吗? 通过进行较小的更改,该功能现在速度提高了四倍,同时保持99.99%的准确性。 我一口气没有得到4倍的加速。 我首先观察了迭代次数与x值之间的关系 通过可视化,我不仅了解了这种关系,而且了解了它对性能的影响。 仅优化功能是不够的,我必须确保功能为所有测试用例提供正确的输出,我必须确保功能的正确性。 使用测试功能,我知道我的迭代优化存在问题。 我编写了通用函数来查找运行时,加快运行速度并比较迭代。 正如在“看过的和看不见的第1部分”中讨论的那样,好的软件是可维护的,可确保正确性和可靠性。 这是看不见的部分。 通过测试,我确保了正确性,编写通用代码确保了可维护性。 可视化有助于理解这些看不见的方面,从而有助于使观看效果更好。

To be honest for a long time I would just write a solution for a given problem and test it for a few cases and move on to the next one. This certainly did not mean that my solution was efficient, and worked for all cases. Finding a solution to a problem is similar to buying clothes in a shop. You don’t buy the first piece of clothing that you come across, you look for something that you like, something that would be easy to maintain- wash, something that is durable and in your budget. Just like that, a solution has to be efficient, easy to maintain, works for any kind of input, and delivers output in a given time frame. So now when I write a program, I look at the unseen part as default, I try to find different ways to optimize it. I test it for numerous test cases and try to make the code as generic as possible. Writing a generic code requires slightly more time and effort but it saves a lot of it in the future while updating the code. This has not only helped me improve my skills but also helped me discover python functionalities than I probably wouldn’t have discovered with my old approach. I hope that you too benefit from my lessons and start looking at the unseen aspects next time you code.

老实说,很长一段时间,我只会为给定问题编写解决方案,并在少数情况下进行测试,然后继续进行下一个。 这当然并不意味着我的解决方案是有效的,并且适用于所有情况。 找到问题的解决方案类似于在商店里买衣服。 您无需购买遇到的第一件衣服,而是寻找自己喜欢的东西,易于维护的东西,经久耐用且预算有限的东西。 如此,解决方案必须高效,易于维护,适用于任何类型的输入并在给定的时间范围内交付输出。 因此,现在当我编写程序时,我将看不见的部分作为默认值,我尝试寻找不同的方法来对其进行优化。 我测试了许多测试用例,并尝试使代码尽可能通用。 编写通用代码需要花费更多的时间和精力,但是将来在更新代码时可以节省很多。 这不仅帮助我提高了技能,而且还帮助我发现了python功能,这是我以前使用的方法可能无法发现的。 我希望您也能从我的课程中受益,并在下次编写代码时开始着眼于看不见的方面。

翻译自: https://medium.com/algoasylum/the-seen-and-the-unseen-part-4-6b40fd7f03a2

函数分解成一系列三角函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值