首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将交替三角函数添加到Python中的数组中?

如何将交替三角函数添加到Python中的数组中?
EN

Stack Overflow用户
提问于 2020-11-08 03:03:21
回答 4查看 149关注 0票数 2

因此,我想在具有给定大小的python中添加交替的Cos和Tan函数。例如,我要为每一行(Tan(a)、Cos(a)、Tan(a)、Cos(a)、Tan(a))添加一个大小为numpy的数组(10-5)。有人能给我一个提示,说明从哪里开始吗?

我尝试在计数器小于5的条件下设置一个使用while循环的计数器,但这看起来不起作用。会很感激你的建议。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-11-08 08:37:14

像这样吗?

代码语言:javascript
运行
复制
arr[:, ::2] = np.tan(arr[:, ::2])
arr[:, 1::2] = np.cos(arr[:, 1::2])

我很大胆,假设您不太熟悉numpy中的切片表示法和索引,所以可以考虑查看docs:Numpy索引

票数 1
EN

Stack Overflow用户

发布于 2020-11-08 03:33:26

解决方案

既然您要求交替,我想给出一个更通用的答案,包括三种类型的替换:

  • 交替row
  • 交替的column你要的这个
  • 交替的rowcolumn (两者兼而有之):像棋盘一样

我将在随后的示例中使用这些虚拟数据:ABC

代码语言:javascript
运行
复制
# dummy data
import numpy as np
angles = np.random.rand(5,4) * np.pi

A.预期产出:交替行

代码语言:javascript
运行
复制
[[tan, tan, tan, tan],
 [cos, cos, cos, cos],
 [tan, tan, tan, tan],
 [cos, cos, cos, cos],
 [tan, tan, tan, tan]]

代码:

代码语言:javascript
运行
复制
# import numpy as np
a = np.zeros(angles.shape)
a[0::2, :] = np.tan(angles[0::2, :])
a[1::2, :] = np.cos(angles[1::2, :])

B.预期产出:交替栏

代码语言:javascript
运行
复制
[[tan, cos, tan, cos],
 [tan, cos, tan, cos],
 [tan, cos, tan, cos],
 [tan, cos, tan, cos],
 [tan, cos, tan, cos]]

代码:

代码语言:javascript
运行
复制
# import numpy as np
a = np.zeros(angles.shape)
a[:, 0::2] = np.tan(angles[:, 0::2])
a[:, 1::2] = np.cos(angles[:, 1::2])

C.预期产出:交替行和列

代码语言:javascript
运行
复制
[[tan, cos, tan, cos],
 [cos, tan, cos, tan],
 [tan, cos, tan, cos],
 [cos, tan, cos, tan],
 [tan, cos, tan, cos]]

代码:

代码语言:javascript
运行
复制
# import numpy as np
a = np.zeros(angles.shape)
a[0::2, 0::2] = np.tan(angles([0::2, 0::2])) # tan: odd-row, odd-column
a[0::2, 1::2] = np.cos(angles([0::2, 1::2])) # cos: odd-row, even-column
a[1::2, 0::2] = np.cos(angles([1::2, 0::2])) # cos: even-row, odd-column
a[1::2, 1::2] = np.tan(angles([1::2, 1::2])) # tan: even-row, even-column

D.交替行使用numpy矢量化的好处

您可以使用以下自定义函数:custom_trig_func()。如果您可以跳过numpy循环,那么将for函数应用于numpy数组会更快。代码越矢量化,运行速度就越快。这里的方法避免了使用for循环,并使用内置的numpy.tannumpy.cos矢量化.

代码语言:javascript
运行
复制
custom_trig_func(angles)

但是,如果您只希望使用最少的代码行来生成自定义函数所做的工作,请使用以下命令:

代码语言:javascript
运行
复制
# angles is your input ndarray
even_rows = (np.arange(angles.shape[0]) % 2 == 0)
out = np.tan(angles)
out[even_rows, :] = np.cos(angles[even_rows, :])
print(out.shape) # out is your expected output

代码

代码语言:javascript
运行
复制
import numpy as np

def custom_trig_func(angles: np.ndarray, validate=False) -> np.ndarray:
    """Returns an array of type -> numpy.ndarray and shape -> angles.shape, with 
    - odd rows operated on by numpy.tan() and, 
    - even rows operated on by numpy.cos().
    """

    # determine odd and even row indices
    rows = np.arange(angles.shape[0])
    even_rows = (rows % 2 == 0)
    odd_rows = ~even_rows

    # create output array
    out = np.tan(angles)
    out[even_rows, :] = np.cos(angles[even_rows, :])

    if validate:
        assert np.all(out[odd_rows, :] == np.tan(angles[odd_rows, :])), "Validation error in applying TAN to odd rows"
        assert np.all(out[even_rows, :] == np.cos(angles[even_rows, :])), "Validation error in applying COS to even rows"

    return out

# dummy data
angles = np.random.rand(10,5) * np.pi
# apply custom function
custom_trig_func(angles=angles, validate=True)
票数 1
EN

Stack Overflow用户

发布于 2020-11-08 03:43:58

这是你要找的东西吗?

代码语言:javascript
运行
复制
import numpy as np
import math
x=np.array([x for x in range(50)]).reshape(10,5)
y=x.copy()
for i in range(x.shape[0]):
    y[i][0]=math.tan(x[i][0])
    y[i][1]=math.cos(x[i][1])
    y[i][2]=math.tan(x[i][2])
    y[i][3]=math.cos(x[i][3])
    y[i][4]=math.tan(x[i][4])
print(x)
print(y)




[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]
 [40 41 42 43 44]
 [45 46 47 48 49]]

[[ 0  0 -2  0  1]
 [-3  0  0  0  0]
 [ 0  0  0  0  7]
 [ 0  0  3  0  0]
 [ 2  0  0  0 -2]
 [ 0  0 -3  0  0]
 [-6  0  0  0  0]
 [ 0  0  0  0  3]
 [-1  0  2  0  0]
 [ 1  0  0  0 -3]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64734436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档