Applicative与Arrow

本文深入解析了控制应用与箭头操作符在函数式编程中的使用,通过实例展示了如何将多个条件组合并应用于列表过滤,以及如何使用特殊操作符提升普通函数以在复杂上下文中应用。

原文说得非常清楚,不翻译效果可能更好。


进入ghci 

> :m + Control.Applicative Control.Arrow
Control.Applicative Control.Arrow>filter ((>2) &&& (<7) >>> uncurry (&&)) [1..10]
[3,4,5,6]


We have two mysterious operators here, with the following types:
(&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')
(>>>) :: Category cat => cat a b -> cat b c -> cat a c

The Arrow and Category type classes are mostly about things that behave like functions, which of course includes functions themselves, and both instances here are just plain(->). So, rewriting the types to use that:
(&&&) :: (b -> c) -> (b -> c') -> (b -> (c, c'))
(>>>) :: (a -> b) -> (b -> c) -> (a -> c)

The second has a very similar type to (.), the familiar function composition operator; in fact, they're the same, just with arguments swapped. The first is more unfamiliar, but the types again tell you all you need to know--it takes two functions, both taking an argument of a common type, and produces a single function that gives the results from both combined into a tuple.

So, the expression (>2) &&& (<7) takes a single number and produces a pair of Bool values based on the comparisons. The result of this is then fed into uncurry (&&), which just takes a pair of Bools and ANDs them together. The resulting predicate is used to filter the list in the usual manner.


Control.Applicative Control.Arrow>filter ((&&) <$> (>2) <*> (<7)) [1..10]

[3,4,5,6]


We have two mysterious operators, again, with the following types:
(<$>) :: Functor f => (a -> b) -> f a -> f b
(<*>) :: Applicative f => f (a -> b) -> f a -> f b

Observe that the second argument of (<$>) in this case is (>2), which has type (Ord a, Num a) => a -> Bool, while the type of (<$>)'s argument has type f a. How are these compatible?

The answer is that, just as we could substitute (->) for a and cat in the earlier type signatures, we can think of a -> Bool as (->) a Bool, and substitute((->) a) for the f. So, rewriting the types, using ((->) t) instead to avoid clashing with the other type variable a:
(<$>) :: (a -> b) -> ((->) t) a -> ((->) t) b
(<*>) :: ((->) t) (a -> b) -> ((->) t) a -> ((->) t) b

Now, putting things back in normal infix form:
(<$>) :: (a -> b) -> (t -> a) -> (t -> b)
(<*>) :: (t -> (a -> b)) -> (t -> a) -> (t -> b)

The first turns out to be function composition, as you can observe from the types. The second is more complicated, but once more the types tell you what you need--it takes two functions with an argument of a common type,one producing a function, the other producing an argument to pass to the function. In other words, something like \f g x -> f x (g x). (This function also happens to be known as the S combinator in combinatory logic, a subject explored extensively by the logician Haskell Curry, whose name no doubt seems strangely familiar!)

The combination of (<$>) and (<*>) sort of "extends" what (<$>) alone does, which in this case means taking a function with two arguments, two functions with a common argument type, applying a single value to the latter two, then applying the first function to the two results. So ((&&) <$> (>2) <*> (<7)) x simplifies to(&&) ((>2) x) ((<7) x), or using normal infix style, x > 2 && x < 7. As before, the compound expression is used to filter the list in the usual manner.

Also, note that while both functions are obfuscated to some degree, once you get used to the operators used, they're actually quite readable. The first abstracts over a compound expression doing multiple things to a single argument, while the second is a generalized form of the standard "pipeline" style of stringing things together with function composition.

Personally I actually find the first one perfectly readable. But I don't expect most people to agree!


总结

Applicatives are great when you've got a plain old function of several variables, and you have the arguments but they're wrapped up in some kind of context. For instance, you have the plain old concatenate function (++) but you want to apply it to 2 strings which were acquired through I/O. Then the fact that IO is an applicative functor comes to the rescue:

Prelude Control.Applicative> (++) <$> getLine <*> getLine 

hi
there
"hithere"


The basic idea is that you're "lifting" a regular function into a context where it can be applied to as many arguments as you like. The extra power of Applicative over just a basic Functor is that it can lift functions of arbitrary arity, whereas fmap can only lift a unary function.



内容概要:本文系统介绍了物理信息神经网络(PINNs)在求解布洛赫-托雷(Bloch-Torrey)方程中的应用,结合PyTorch框架提供了完整的Python代码实现案例。文章深入阐述了如何将物理先验知识嵌入神经网络训练过程,通过构建复合损失函数,强制网络输出满足控制方程、初始条件边界条件,从而实现对布洛赫-托雷方程的无网格化、高精度求解。该方法突破了传统数值方法在高维、多尺度及复杂几何场景下的计算瓶颈,展现出优异的泛化能力计算效率,特别适用于医学成像、扩散磁共振等领域中复杂的物理场建模仿真任务。; 适合人群:具备深度学习偏微分方程理论基础,从事科学计算、生物医学工程、材料科学或相关交叉学科研究的研究生、科研人员及算法工程师。; 使用场景及目标:①应用于扩散磁共振成像(dMRI)等医学影像技术中的复杂扩散过程建模反演;②为高维偏微分方程的高效求解提供数据驱动的新范式,提升仿真精度计算速度;③作为PINNs在AI for Science领域中的典型实践案例,推动物理引导的深度学习方法在实际科研项目中的落地拓展。; 阅读建议:建议读者结合提供的完整代码资源(可通过公众号“荔枝科研社”或百度网盘获取),动手复现并调试模型,深入理解PINNs的架构设计、损失函数构建物理约束嵌入机制,同时可尝试将该方法迁移至其他类似物理系统的建模求解任务中进行创新性研究。
内容概要:本文围绕“基于多VSG独立微网的多目标二次控制MATLAB模型研究”展开,详细阐述了利用Simulink对多虚拟同步发电机(VSG)构成的独立微网系统进行建模仿真,实现频率调节、电压支撑有功无功功率均分等多目标协同优化的二次控制策略。研究引入先进的最优控制算法,解决微网在孤岛运行模式下的功率动态分配、频率电压恢复及系统稳定性问题,并通过MATLAB/Simulink平台构建完整仿真模型,验证所提控制策略在不同负载扰动下的有效性、鲁棒性动态响应性能。; 适合人群:具备电力系统分析、现代控制理论基础以及MATLAB/Simulink仿真能力的电气工程、自动化等相关专业的硕士研究生、科研人员及从事微网控制系统开发的工程技术人才。; 使用场景及目标:① 深入理解多VSG在独立微网中的并联运行机理协同控制架构;② 掌握基于Simulink的微网二次控制系统的建模方法仿真流程;③ 实现频率、电压功率分配的多目标优化控制仿真验证;④ 为微网控制系统的设计、算法优化及科研课题提供可靠的仿真依据和技术参考。; 阅读建议:建议读者结合文中控制策略,动手搭建Simulink模型,重点关注控制器参数整定对系统动态性能的影响,可通过对比不同工况下的仿真结果,进一步优化控制算法以提升系统鲁棒性响应精度。
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进行下载。 另外,本资源描述中并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!
代码下载地址: https://pan.quark.cn/s/a4b39357ea24 编写程序,建立容量为n(建议n=8)的循环队列,完成以下程序功能。 输入字符#,执行一次出队操作,屏幕上显示出队字符;输入字符@,队列中所有字符依次出队并按出队次序在屏幕上显示各字符;输入其它字符,则输入的字符入队。 要求采用队头/队尾间隔至少一个空闲元素的方法来实现循环队列;空队执行出队操作及队满执行入队操作需显示提示信息。 ### 数据结构实验报告知识点 #### 实验背景目标 本次实验是关于数据结构中的队列基本操作算法。 队列是一种先进先出(FIFO)的数据结构,在计算机科学中有着广泛的应用,例如进程调度、任务队列等场景。 通过本实验,学生能够深入理解循环队列的概念,并熟练掌握其实现方法。 #### 实验要求内容 1. **实验内容**:要求编写一个程序来建立容量为 _n_ 的循环队列(推荐 _n_ = 8),并实现以下功能: - 输入字符 `#` 执行一次出队操作,并显示该出队字符; - 输入字符 `@`,将队列中的所有字符依次出队,并按照出队顺序在屏幕上显示这些字符; - 输入其他任意字符,则将该字符入队。 2. **特殊要求**: - 采用队头/队尾间隔至少一个空闲元素的方法实现循环队列,这样可以避免队列的物理连续性逻辑连续性的混淆,同时便于检测队列是否为空或满。 - 当队列为满时尝试执行入队操作,或者队列为时空执行出队操作时,需要给出相应的提示信息。 3. **注意事项**: - 在反复输入字符时,应妥善处理输入缓冲区中的回车键(即 `\n` 字符)的问题,避免因连续输入导致的错误行为。 #### 数据结构设计 为了实现上述要求,本实验采用了如下的数据结构设计: ...
内容概要:本文提出了一种基于数据驱动的Koopman算子递归神经网络(RNN)相结合的模型线性化方法,用于提升纳米定位系统的预测控制性能。该方法通过Koopman算子将复杂的非线性系统动态映射至高维线性空间,克服传统建模在强非线性条件下的局限性,再结合RNN强大的时序特征捕捉能力,实现对系统未来状态的高精度预测有效控制。整个框架完全基于数据驱动,无需精确物理建模,特别适用于原子力显微镜、半导体制造等对定位精度要求极高的应用场景,并通过Matlab代码实现了算法的完整仿真验证。; 适合人群:具备控制理论基础和Matlab编程能力,从事精密运动控制、智能算法开发、非线性系统建模预测控制研究的研究生、科研人员及工程技术开发者。; 使用场景及目标:①解决纳米级定位平台中存在的强非线性、迟滞、蠕变等复杂动态特性带来的控制难题;②为高精度机电系统提供一种可复现、易实现的数据驱动预测控制方案;③推动Koopman理论深度学习在先进制造智能控制领域的深度融合应用创新。; 阅读建议:建议读者结合提供的Matlab代码深入理解Koopman算子的数值实现流程RNN网络结构设计细节,重点关注模型在不同工况下的泛化能力、实时性表现及控制稳定性,可进一步将其拓展至其他高精度伺服控制系统的研究优化中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值