Middle-题目20:153. Find Minimum in Rotated Sorted Array

本文介绍了一种针对已知无重复元素的移位排序数组寻找最小值的方法。通过一次遍历实现,确保了高效性,并附带了实际运行效率评估。

题目原文:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.
题目大意:
对一个移位后的数组,求最小值。
题目分析:
遍历一遍数组无脑找。
源码(略)
成绩:
1ms,beats 2.14%,众数1ms,92.29%

需求响应动态冰蓄冷系统与需求响应策略的优化研究(Matlab代码实现)内容概要:本文围绕需求响应动态冰蓄冷系统及其优化策略展开研究,结合Matlab代码实现,探讨了在电力需求侧管理背景下,冰蓄冷系统如何通过优化运行策略参与需求响应,以实现削峰填谷、降低用电成本和提升能源利用效率的目标。研究内容包括系统建模、负荷预测、优化算法设计(如智能优化算法)以及多场景仿真验证,重点分析不同需求响应机制下系统的经济性和运行特性,并通过Matlab编程实现模型求解与结果可视化,为实际工程应用提供理论支持和技术路径。; 适合人群:具备一定电力系统、能源工程或自动化背景的研究生、科研人员及从事综合能源系统优化工作的工程师;熟悉Matlab编程且对需求响应、储能优化等领域感兴趣的技术人员。; 使用场景及目标:①用于高校科研中关于冰蓄冷系统与需求响应协同优化的课题研究;②支撑企业开展楼宇能源管理系统、智慧园区调度平台的设计与仿真;③为政策制定者评估需求响应措施的有效性提供量化分析工具。; 阅读建议:建议读者结合文中Matlab代码逐段理解模型构建与算法实现过程,重点关注目标函数设定、约束条件处理及优化结果分析部分,同时可拓展应用其他智能算法进行对比实验,加深对系统优化机制的理解。
class CH_R(Directivity): """ Real cylindrical harmonics are a set of harmonic sine and cosine functions that are periodic with period 2pi. This class defines Directivities whose directional responses are cylindrical harmonics. Parameters ---------- order, degree : int Index to this cylindrical harmonics. ind : int Single-integer index, used if both order and degree are None. normalized : bool Specifies whether the directional response is normalized (i.e. unit mean square value). """ def __init__(self, order = None, degree = None, ind = None, normalized = True, **kwargs): super().__init__(**kwargs) self.set_normalized(normalized) if order is None and degree is None: order = (ind + 1) // 2 degree = 0 if order == 0 else (ind + 1) % 2 self.order = order self.degree = degree # Override def _get_params(self): return {'order' : self.order, 'degree' : self.degree, 'normalized' : self.normalized} # Override def _get_shortlabel(self): return 'CH' def dir_gain(self, azimuth, colatitude = None, degrees = False): """ Unoriented directional gain (which is a cylindrical harmonics). Parameters ---------- azimuth : array Angles (in radians) to return directional gains for. colatitude, frequency : None Unused. degrees : bool Whether angles are given in degrees. Returns ------- gain : array_like(azimuth) Directional gains in *azimuth*. """ if degrees: azimuth = np.radians(azimuth) if self.order == 0: return np.ones_like(azimuth) if self.degree == 0: return self.h_gain * np.cos(self.order * azimuth) if self.degree == 1: return self.h_gain * np.sin(self.order * azimuth) def get_wxy(self, oriented = True, azim = None, colat = None): """ Parameters *azim* and *colat* are unused. """ if self.order == 0: return np.array([1, 0, 0]) elif self.order == 1: if self.degree == 0: return np.array([0, 1, 0]) elif self.degree == 1: return np.array([0, 0, 1]) elif self.order > 1: if self.degree == 0 or self.degree == 1: return np.array([0, 0, 0]) def set_normalized(self, normalized): self.normalized = normalized if normalized: self.h_gain = np.sqrt(2) else: self.h_gain = 1 @staticmethod def rot_mats(max_order, rad): """ Compute a collection of rotation matrices for mixture-of-CH directivities. Parameters ---------- max_order : int Highest CH order to be rotated. rad : numeric Angle of rotation, in rads. Returns ------- mats : list[array] A list of *max_order* 2x2 matrices that rotate CH weights of orders 1, ..., max_order, respectively. """ if isinstance(rad, Orientation): rad = rad.get_azimuth() mats = [] for o in range(max_order): orad = (o + 1) * rad c = np.cos(orad) s = np.sin(orad) mats.append(np.array([[c, -s], [s, c]])) return mats @staticmethod def rotate(c, mats, axis = 0): """ Rotate one mixture-of-CH directional response to another. In typical usage *c* contains an odd number of channels (1 + 2 * max_order) and *mats* contain at least max_order 2x2 matrices. If less than max_order matrices are available the corresponding high order weights are not rotated. If *c* is even the imcomplete top order is not rotated. Parameters ---------- c : array Array encoding the original directional response as CH weights. By default the first dimension (axis 0) are the CH channels, i.e. c[0] contain weight(s) of the 0th order. This can be reconfigured via *axis*. mats : list[array2d] List of rotation matrices to be applied. See also rot_mats. axis : int Index of the axis that indices CH channels. Returns ------- c_rot : array Array encoding the rotated directional response as CH weights. """ c_rot = c + 0 c_rot = c_rot.swapaxes(0, axis) for o in range(min(len(mats), (len(c_rot) - 1) // 2)): # CH weights of order o + 1 c_o = c_rot[2 * o + 1 : 2 * o + 3] # Rotation matrices are left operators hence the transpose when applied on the right. # c_o[:] = c_o.T.dot(mats[o].T).T c_o[:] = np.tensordot(mats[o], c_o, axes=1) return c_rot.swapaxes(0, axis) @staticmethod def index_to_orderdegree(index): order = (index + 1) // 2 degree = 0 if index == 0 else (index - 1) // 2 return order, degree
12-18
<think>我们分析这个类 `CH_R`,它是 `Directivity` 的子类,用于表示圆柱谐函数(Cylindrical Harmonics)的方向特性。圆柱谐函数在二维平面(忽略高度)的声场分析中常用,例如在Ambisonics中。 ### 类功能概述 1. **初始化**:根据阶数(order)和度数(degree)或单一索引(ind)来创建圆柱谐函数的方向性模式。 2. **方向增益计算**:根据给定的方位角(azimuth)计算该方向上的增益(即圆柱谐函数的值)。 3. **获取WXY表示**:对于低阶(0阶和1阶)返回对应的Ambisonics B格式的W、X、Y分量(忽略Z,因为是2D)。 4. **归一化设置**:可以设置是否对方向性进行归一化(归一化系数为sqrt(2))。 5. **旋转工具**:提供静态方法计算旋转矩阵,并对一组圆柱谐函数的系数进行旋转。 ### 代码实现解析 #### 初始化方法 `__init__` - 参数:`order`(阶数),`degree`(度数,0表示余弦分量,1表示正弦分量),`ind`(单一索引,用于代替order和degree),`normalized`(是否归一化)。 - 如果`order`和`degree`都为None,则使用`ind`计算它们:阶数`order = (ind+1)//2`,度数`degree`在0阶时为0,否则为`(ind+1)%2`(实际上,当ind=0时,order=0, degree=0;ind=1时,order=1, degree=0;ind=2时,order=1, degree=1;以此类推)。 - 设置归一化标志和归一化系数(通过`set_normalized`方法)。 #### 方向增益计算方法 `dir_gain` - 输入:方位角`azimuth`(必须,单位可以是弧度或度),`colatitude`(未使用,因为圆柱谐函数是2D的),`degrees`(指示角度单位)。 - 根据阶数和度数计算增益: - 0阶:全向,增益为1。 - 阶数不为0且度数为0:`h_gain * cos(order * azimuth)` - 度数为1:`h_gain * sin(order * azimuth)` - 归一化系数`h_gain`在归一化时为`sqrt(2)`,否则为1。 #### 获取WXY表示 `get_wxy` - 返回一个三维向量,分别对应Ambisonics B格式的W、X、Y(因为圆柱谐函数用于2D,所以没有Z)。 - 0阶:返回[1,0,0](即W分量)。 - 1阶且度数为0:返回[0,1,0](X分量)。 - 1阶且度数为1:返回[0,0,1](Y分量)。 - 高阶(阶数>1):返回[0,0,0](因为低阶Ambisonics只包含0阶和1阶)。 #### 归一化设置 `set_normalized` - 设置`normalized`属性,并根据是否归一化设置`h_gain`(归一化时为`sqrt(2)`,否则为1)。 #### 旋转矩阵工具 `rot_mats` - 静态方法,计算从1阶到`max_order`阶的旋转矩阵(每个阶一个2x2的旋转矩阵)。 - 旋转角度`rad`可以是一个数值,也可以是一个`Orientation`对象(此时取其方位角)。 - 每个阶数`o`(从0到max_order-1,实际对应阶数o+1)的旋转矩阵为: ```python [[cos((o+1)*rad), -sin((o+1)*rad)], [sin((o+1)*rad), cos((o+1)*rad)]] ``` #### 旋转系数 `rotate` - 静态方法,使用旋转矩阵`mats`旋转圆柱谐函数的系数数组`c`。 - 系数数组`c`的指定轴(默认为0轴)上按顺序存储了从0阶开始的各阶系数。0阶1个,1阶2个(余弦和正弦),2阶2个,...,总共1+2*max_order个。 - 旋转过程:对于每个阶数(从1阶开始),取对应的两个系数(余弦和正弦分量),用该阶对应的旋转矩阵进行变换(矩阵乘法)。 - 注意:高阶的系数(超过提供的旋转矩阵个数)不旋转,0阶不旋转。 #### 索引转换为阶数和度数 `index_to_orderdegree` - 静态方法,根据索引`index`计算阶数`order`和度数`degree`。 - `order = (index+1)//2` - `degree = 0 if index==0 else (index-1)//2` # 注意:这个计算与初始化中的逻辑不同,可能存在错误?下面分析。 注意:在初始化中,当使用`ind`时,计算方式为: - `order = (ind+1)//2` - `degree = 0 if order==0 else (ind+1)%2` 而`index_to_orderdegree`方法中: - `order = (index+1)//2` - `degree = 0 if index==0 else (index-1)//2` 这显然不一致。例如,当index=1时: - 初始化:order=(1+1)//2=1, degree= (1+1)%2=0 -> (1,0) - 此方法:order=(1+1)//2=1, degree=(1-1)//2=0 -> (1,0) 相同。 当index=2时: - 初始化:order=(2+1)//2=1(整数除法,结果为1),degree=(2+1)%2=1 -> (1,1) - 此方法:order=(2+1)//2=1, degree=(2-1)//2=0(整数除法,1//2=0)-> (1,0) 不一致。 因此,这个方法可能存在错误。正确的应该与初始化一致:对于index,阶数`order=(index+1)//2`,度数`degree`在阶数不为0时等于`(index+1) % 2`,但注意当index=0时,阶数0,度数0。 实际上,这个方法可能没有使用,或者设计意图不同?根据函数名,它应该是将线性索引映射到(order, degree),而我们的索引规则是: index=0 -> (0,0) index=1 -> (1,0) index=2 -> (1,1) index=3 -> (2,0) index=4 -> (2,1) ... 所以,正确的映射应该是: order = (index==0) ? 0 : (index+1)//2 # 不对,因为index=1和2对应order=1,index=3和4对应order=2。 order = (index+1)//2 # 这个对于index=0: (1//2)=0, index=1: (2//2)=1, index=2: (3//2)=1(整数除法),index=3: (4//2)=2 -> 正确。 度数:每个阶数有两个度数(0和1),所以对于给定的阶数order,第一个通道(index=2*order-1)是余弦(degree=0),第二个(index=2*order)是正弦(degree=1)。因此,度数应该是:当index=0时,度数为0;当index>=1时,度数=(index-1) % 2。但是注意,index=1: (1-1)%2=0,index=2: (2-1)%2=1,index=3: (3-1)%2=0,index=4: (4-1)%2=1,正确。 所以,`index_to_orderdegree`方法应改为: order = (index+1)//2 degree = 0 if index==0 else (index-1) % 2 但当前代码中写的是`(index-1)//2`,这会导致错误(比如index=2时,得到0,但应该是1)。 因此,这个静态方法可能是一个错误,或者它用于不同的索引方案?由于在初始化中已经使用了另一种方法,所以这个静态方法可能并未使用,或者设计有误。 ### 总结 这个类主要用于2D Ambisonics(圆柱谐函数)的方向性建模,提供了方向增益计算、系数旋转等功能。注意静态方法`index_to_orderdegree`可能存在错误。 §§相关问题§§ 1. 圆柱谐函数(CH)与球谐函数(SH)在声场表示上有何主要区别? 2. 在`dir_gain`方法中,为什么高阶(>1阶)圆柱谐函数在`get_wxy`中返回零向量? 3. 如何修正`index_to_orderdegree`方法以正确映射索引到阶数和度数? 4. 旋转矩阵`rot_mats`为什么需要针对不同阶数分别计算? 5. 在实时音频处理中,如何利用`CH_R`类实现声场旋转效果? </think> 这个代码定义了一个**圆柱谐函数(Cylindrical Harmonics)方向性模型**类,用于声学方向性建模和Ambisonics处理。以下是核心解析: ### 功能概述 实现圆柱谐函数(2D平面声场)的方向性模型,主要用于: 1. 描述麦克风/扬声器的方向性模式 2. 实现Ambisonics B格式的2D音频处理 3. 支持方向性旋转和坐标变换 ### 关键方法解析 ```mermaid graph TD A[CH_R] --> B[初始化] A --> C[方向增益计算] A --> D[WXY分量获取] A --> E[归一化设置] A --> F[旋转矩阵计算] A --> G[系数旋转] ``` #### 1. 初始化 `__init__` - **参数转换**:支持两种索引方式 ```python if order is None: order = (ind + 1) // 2 # 从单索引计算阶数 degree = 0 if order==0 else (ind + 1) % 2 # 计算度数 ``` - **归一化**:`h_gain = √2` (归一化时) 或 `1` (非归一化) #### 2. 方向增益计算 `dir_gain` 计算指定方位角的方向响应: ```python def dir_gain(azimuth): if order==0: return 1 # 0阶:全向 if degree==0: return h_gain * cos(order*azimuth) # 余弦分量 if degree==1: return h_gain * sin(order*azimuth) # 正弦分量 ``` #### 3. WXY分量获取 `get_wxy` 返回Ambisonics B格式的2D分量: - 0阶 → [1,0,0] (W通道) - 1阶余弦 → [0,1,0] (X通道) - 1阶正弦 → [0,0,1] (Y通道) #### 4. 旋转处理核心 - **旋转矩阵生成** `rot_mats`: ```python for o in range(max_order): orad = (o+1)*rad mats.append([[cos(orad), -sin(orad)], [sin(orad), cos(orad)]]) ``` - **系数旋转** `rotate`: ```python for o in range(min(len(mats), (len(c_rot)-1)//2)): c_o = c_rot[2*o+1 : 2*o+3] # 提取当前阶数系数 c_o[:] = np.tensordot(mats[o], c_o, axes=1) # 矩阵旋转 ``` ### 圆柱谐函数特性 | 阶数 | 度数 | 物理意义 | 方向模式 | |------|------|----------|----------| | 0 | 0 | 全向声压 | 圆形 | | 1 | 0 | 前后方向 | 8字形(cos) | | 1 | 1 | 左右方向 | 8字形(sin) | | n | 0/1 | 高阶方向性 | 多瓣形 | ### 应用场景 - 2D Ambisonics编码/解码 - 声场旋转处理 - 方向性麦克风阵列建模 - VR音频中的头部旋转补偿
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值