不确定for循环迭代?(Unsure about for-loop iteration?)
所以我刚开始用Java编程,我只是很难理解为什么会这样
for (int i = 0, j=0; i <10; i++) { System.out.println(j += j++); }
打印出10次?
So I'm new to programming in Java and I'm just having a hard time understanding why this
for (int i = 0, j=0; i <10; i++) { System.out.println(j += j++); }
prints out 0 ten times?
原文:
更新时间:2022-04-27 10:04
最满意答案
通过使用产品与总和触发器。 身份 ,你可以减少三角形的数量。 函数调用。 在下面,
func1
和func2
计算相同的值,但func2
减少了触发的调用。 功能。import numpy as np def func1(a, b, c, d): A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d) return A def func2(a, b, c, d): s = np.sin(c - d) A = 0.5*((1 - s)*np.cos(a - b) + (1 + s)*np.cos(a + b)) return A
这是
N = 5000
的时序比较:In [48]: %timeit func1(a, b, c, d) 1000 loops, best of 3: 374 µs per loop In [49]: %timeit func2(a, b, c, d) 1000 loops, best of 3: 241 µs per loop
By using the product-to-sum trig. identities, you can reduce the number of trig. function calls. In the following,
func1
andfunc2
compute the same value, butfunc2
makes fewer calls to trig. functions.import numpy as np def func1(a, b, c, d): A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d) return A def func2(a, b, c, d): s = np.sin(c - d) A = 0.5*((1 - s)*np.cos(a - b) + (1 + s)*np.cos(a + b)) return A
Here's a timing comparison with
N = 5000
:In [48]: %timeit func1(a, b, c, d) 1000 loops, best of 3: 374 µs per loop In [49]: %timeit func2(a, b, c, d) 1000 loops, best of 3: 241 µs per loop
相关问答
更多-
下面是关于如何做三角函数的幂级数近似(不是泰勒级数)的一些很好的幻灯片: http : //www.research.scea.com/gdc2003/fast-math-functions.html 它面向游戏程序员,这意味着准确性会牺牲性能,但是您应该能够在近似值中添加另外一个或两个术语,以获得一些准确度。 关于这一点的好处是,您还应该能够轻松地将其扩展到SIMD,以便您可以在一个(如果使用双精度时为2)计算4个值的正弦或余弦。 希望这有助于... Here are some good slides o ...
-
快速三角函数(cos,tan,arcsin,arcos,arctan)(swift trigonometric functions (cos, tan, arcsin, arcos, arctan))[2023-09-07]
这是一个数学问题,而不是Swift问题: let sinus = sin(90.0 * M_PI / 180) print("Sinus \(sinus)") let cosinus = cos(90 * M_PI / 180) print("Cosinus \(cosinus)") let tangent = tan(90 * M_PI / 180) print("Tangent \(tangent)") 版画 Sinus 1.0 Cosinus 6.12323399573677e-17 Tange ... -
三角函数如何工作?(How do Trigonometric functions work?)[2023-01-26]
首先,你必须做一些减小范围。 Trig函数是周期性的,因此您需要将参数减少到标准间隔。 对于初学者,您可以将角度减小到0到360度之间。 但是通过使用几个身份,你意识到你可以得到更少的。 如果您计算0和45度之间角度的正弦和余弦,则可以引导您计算所有角度的所有触发功能。 一旦你减少了争论,大多数芯片都使用CORDIC算法来计算正弦和余弦。 你可能会听到有人说电脑使用泰勒系列。 这听起来很合理,但不是这样。 CORDIC算法更适合高效的硬件实现。 ( 软件库可能使用泰勒系列,在不支持触发功能的硬件上说)。可能 ... -
聚合numpy功能(Aggregate numpy functions)[2022-08-22]
当使用浮点数组时,可以使用np.einsum - np.sqrt(np.einsum('ij,ij->i',a,a)) 运行时测试 - In [34]: a = np.random.rand(1000,1000) In [35]: np.allclose(np.sum(a**2, axis=1)**.5,np.sqrt(np.einsum('ij,ij->i',a,a))) Out[35]: True In [36]: %timeit np.sum(a**2, axis=1)**.5 100 loop ... -
不到Radians <==> toDegrees()工作吗? 您可以使用这些方法将度数转换为弧度,反之亦然 Doesn't toRadians <==> toDegrees() work ? You can use these methods to convert from degrees to radians and the other way around
-
1.2246467991473532e-16接近于0 - 小数点和第一个有效数字之间有16个零 - 很像3.1415926535897931 ( math.pi的值)接近于pi。 答案是正确的小数点后16位! 所以如果你想让sin(pi)等于0,只需将其舍入到合理的小数位数。 15对我来说看起来不错,对于任何应用程序都应该足够多: print round(math.sin(math.pi), 15) 1.2246467991473532e-16 is close to 0 -- there are 16 ...
-
根据所需的速度和精度要求,您可以通过编写程序来创建查找表,从而创建简单查找表所需的功能。 或者使用CORDIC 。 Depending on the needed requirements for speed and precision, maybe you could create the functions needed by a simple lookup table, by writing a program to create the lookup table. Or use CORDIC.
-
问题的范围很广,但这里有一些简单的想法(和代码!)可以作为计算arctan的起点。 首先,好老泰勒系列。 为简单起见,我们使用固定数量的术语; 实际上,您可能希望根据x的大小来决定动态使用的术语数,或者引入某种收敛准则。 使用固定数量的术语,我们可以使用类似于Horner方案的东西进行有效评估。 def arctan_taylor(x, terms=9): """ Compute arctan for small x via Taylor polynomials. Uses a f ...
-
限制通过三角函数的角度有没有优势?(Is there any advantage to restraining the angle passed to trigonometric functions?)[2023-04-28]
由于用于计算三角函数的大多数(on-die)算法使用CORDIC的一些变体,我敢打赌,无论如何,这些值在trig函数调用的入口点处被限制在[0,Pi / 2)之内。 这就是说,如果你有一种方法可以在整个算法中保持角度接近于零,那么这样做可能是明智的。 实际上,sin(10 ^ 42)的值几乎是不确定的,因为10 ^ 42范围内的粒度大约为10 ^ 25。 这意味着例如如果你要增加角度,并且如果这样做,它们可以变大,那么你应该考虑定期夹紧它们。 但在三角函数调用之前将它们夹紧是不必要的。 Since most ... -
通过使用产品与总和触发器。 身份 ,你可以减少三角形的数量。 函数调用。 在下面, func1和func2计算相同的值,但func2减少了触发的调用。 功能。 import numpy as np def func1(a, b, c, d): A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d) return A def func2(a, b, c, d): s = np.sin(c - d) ...