scheme 矩阵运算

基于表:

( define ( accumulate op init seq )

( cond

[ ( null? seq ) init ]

[ else

( op ( car seq )

( accumulate op init ( cdr seq ) ) ) ] ) )

( define ( accumulate-n op init seqs )

( cond

[ ( null? ( car seqs ) ) ‘() ]

[ else

( cons ( accumulate op init ( map car seqs ) )

( accumulate-n op init ( map cdr seqs ) ) ) ] ) )

( define ( dot-product v m )

( accumulate + 0 ( map * v m ) ) )

( define ( mat-*-vec mat vec )

( map ( lambda ( row )( dot-product row vec ) ) mat ) )

( define ( transpose mat )

( accumulate-n cons ‘() mat ) )

( define ( mat-*-mat m n )

( let ( [ n ( transpose n ) ] )

( map ( lambda ( row )( mat-*-vec n row ) ) m ) ) )

基于vector:

#!r6rs

( import ( rnrs ) )

( define type-error

( lambda ( what )

( assertion-violation ‘mul "not a number or matrix" what ) ) )

( define match-error

( lambda ( what1 what2 )

( assertion-violation ‘mul "incompatible operands" what1 what2 ) ) )

( define make-matrix

( lambda ( rows cols )

( do ( [ mat ( make-vector rows ) ]

[ r 0 ( + r 1 ) ] )

( ( = r rows ) mat )

( vector-set! mat r ( make-vector cols ) ) ) ) )

( define matrix?

( lambda ( mat )

( and ( vector? mat )

( vector? ( vector-ref mat 0 ) )

( > ( vector-length mat ) 0 ) ) ) )

( define matrix-ref

( lambda ( mat row col )

( vector-ref ( vector-ref mat row ) col ) ) )

( define matrix-set!

( lambda ( mat row col val )

( vector-set! ( vector-ref mat row ) col val ) ) )

( define matrix-rows ( lambda ( mat )( vector-length mat ) ) )

( define matrix-cols ( lambda ( mat )( vector-length ( vector-ref mat 0 ) ) ) )

( define mat-*-vec

( lambda ( mat vec )

( let* ( [ rows ( matrix-rows mat ) ]

[ cols ( matrix-cols mat ) ]

[ res ( make-matrix rows cols ) ] )

( do ( [ r 0 ( + r 1 ) ] )

( ( = r rows ) res )

( do ( [ c 0 ( + c 1 ) ] )

( = c cols )

( vector-set! res r c ( * vec ( matrix-ref mat r c ) ) ) ) ) ) ) )

( define mat-*-mat

( lambda ( mat1 mat2 )

( let* ( [ rows1 ( matrix-rows mat1 ) ]

[ cols1 ( matrix-cols mat1 ) ]

[ rows2 ( matrix-rows mat2 ) ]

[ cols2 ( matrix-cols mat2 ) ]

[ res ( make-matrix rows1 cols2 ) ] )

( unless ( = cols1 rows2 )

( match-error mat1 mat2 ) )

( do ( [ r 0 ( + r 1 ) ] )

( ( = r rows1 ) res )

( do ( [ c 0 ( + c 1 ) ] )

( ( = c cols2 ) )

( do ( [ k 0 ( + k 1 ) ]

[ val 0 ( + val

( * ( matrix-ref mat1 r k )

( matrix-ref mat2 k c ) ) ) ] )

( ( = k rows2 )

( matrix-set! res r c val ) ) ) ) ) ) ) )

( define mul

( lambda ( x y )

( cond

[ ( number? x )

( cond

[ ( number? y )( * x y ) ]

[ ( matrix? y )( mat-*-vec y x ) ]

[ else ( type-error y ) ] ) ]

[ ( matrix? x )

( cond

[ ( number? y )( mat-*-vec x y ) ]

[ ( matrix? x )( mat-*-mat x y ) ]

[ else ( type-error x ) ] ) ]

[ else ( type-error x ) ] ) ) )

时间: 2024-08-08 01:27:40

scheme 矩阵运算的相关文章

Xcode6.4注册URL Scheme步骤详解

URL Scheme的作用 我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的.但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便app之间互相调用而设计的.我们可以通过系统的OpenURL来打开该app,并可以传递一些参数. 例如:你在Safari里输入www.alipay.com,就可以直接打开你的支付宝app,前提是你的手机装了支付宝.如果你没有装支付宝,应该显示的是支付宝下载界面,点击会跳到AppStore的支付宝下载

(已解决)#warning:尚未配置[微信]URL Scheme:wx4868b35061f87884, 无法使用进行授权。

#warning:尚未配置[微信]URL Scheme:wx4868b35061f87884, 无法使用进行授权. (说白了就是注册白名单) " -canOpenURL: failed for URL: "weixin://app/wx4868b35061f87885/" - error: "This app is not allowed to query for scheme weixin"  " 此error源自iOS9 URL Scheme

Vim Color Scheme

Molokai Color Scheme for Vim I prefer molokai's dark grey background. Installation with Pathogen cd ~/.vim/bundle && \git clone https://github.com/tomasr/molokai.git Now Molokai is installed. Add the following to your vimrc. " Set color schem

全民Scheme(0):lat的定义

接下来我会写一写Scheme的学习笔记.嗯,Scheme是属于小众的语言,但合适用来教学的. 什么是lat,就是遍历list里的每个S-expression,如果发现其中某个不是atom的,则返回false,否则返回true. (define atom? (lambda (x) (and (not (pair? x)) (not (null? x))))) (define lat? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (lat? (

python矩阵运算 不断收集整理

python矩阵运算 转自:http://blog.sina.com.cn/s/blog_5f234d4701012p64.html Python使用NumPy包完成了对N-维数组的快速便捷操作.使用这个包,需要导入numpy.SciPy包以NumPy包为基础,大大的扩展了numpy的能力.为了使用的方便,scipy包在最外层名字空间中包括了所有的numpy内容,因此只要导入了scipy,不必在单独导入numpy了!但是为了明确哪些是numpy中实现的,哪些是scipy中实现的,本文还是进行了区

[Scheme入门]3 高阶函数

 1.高阶函数的介绍 高阶函数的英文名称是Higher Order Function,它们是以函数为参数的函数.主要用于映射(mapping).过滤(filtering).归档(folding)和排序(sorting)表.高阶函数让程序更具模块性,让函数更加通用. 函数sort具有2个参数,一个是需要排序的表,另一个是定序(Ordering)函数.下面展示了按照大小将一个整数表正序排序.而<函数就是本例中函数的定序函数. (sort'(420 -130 138 983 0 298 783 -

scheme 符号求导程序

SICP 习题: #lang scheme ( define ( variable? x ) ( symbol? x ) ) ( define ( same-variable? x y ) ( and ( variable? x ) ( variable? y ) ( eq? x y ) ) ) ( define ( =number? exp num ) ( and ( number? exp ) ( = exp num ) ) ) ;;;;;;;;;;;;;;;;; sum ;;;;;;;;;

spark程序异常:Exception in thread &quot;main&quot; java.io.IOException: No FileSystem for scheme: hdfs

命令: java -jar myspark-1.0-SNAPSHOT.jar myspark-1.0-SNAPSHOT.jar hdfs://single:9000/input/word.txt hdfs://single:9000/output/out1 错误信息: .......... 14/11/23 06:14:18 INFO SparkDeploySchedulerBackend: Granted executor ID app-20141123061418-0011/0 on hos

slime for mit scheme

很多人写过 Scheme 开发环境的设置.但 Scheme 毕竟是一种 Lisp ,是 Lisp 就应该用 Slime. Chicken Scheme 有一个扩展叫 Chicken Slime, 安装非常简单,但功能极差.安装之后几乎没用. Slime本身带的,contrib目录下的swank-kawa.scm swank-larceny.scm swank-mit-scheme.scm 一个都不能用. 好在这位先生修复了对Mit Scheme的支持.照他指导安装就可以用了. 但他说 "也可以将