首页 \ 问答 \ J:Gauss-Jordan淘汰赛(J: Gauss-Jordan elimination)

J:Gauss-Jordan淘汰赛(J: Gauss-Jordan elimination)

编码Gauss-Jordan方法求解代数方程线性系统的任务是我选择推进学习的一个练习J.系统是Ax = b ,其中An- by- n矩阵, b和未知xn - 载体。 首先,我从最简单的控件结构开始:

   gj0 =: dyad :0 NB. usage is same to %.
    y=.y,.b 
    for_d.i.#y do. 
     for_r.i.#y do. 
      if.r=d do.continue.end. NB. do not eliminate d'th row
      t=.%/ (<"1(r,d),:(d,d)) { y
      for_c.d}.>:i.#y do.
       y=.(((<r,c){y)-(t*(<d,c){y)) (<r,c)} y
      end.
      y=.0 (<r,d)} y NB. ensure zero
     end. 
    end. NB. now A is diagonal but not identity matrix, so:
    x=.{:"1 y NB. x = b
    for_r.i.#y do.
     x=.((r{x)%(<r,r){y) r} x NB. divide by coefficients on diagonal
    end. 
   )
   Ab =: (".;._2) 0 :0
   0.25 _0.16 _0.38  0.17
   0.19 _0.22 _0.02  0.41
   0.13  0.08 _0.08 _0.13
   0.13 _0.1  _0.32  0.65
   )
   b =: 0.37 0.01 0.01 1.51
   (,.".&.>)('A';'b';'gj0 A,.b';'b %. A')
┌────────┬──────────────────────┐
│A       │0.25 _0.16 _0.38  0.17│
│        │0.19 _0.22 _0.02  0.41│
│        │0.13  0.08 _0.08 _0.13│
│        │0.13  _0.1 _0.32  0.65│
├────────┼──────────────────────┤
│b       │0.37 0.01 0.01 1.51   │
├────────┼──────────────────────┤
│b gj0 A │_1 3 _2 2             │
├────────┼──────────────────────┤
│b %. A  │_1 3 _2 2             │
└────────┴──────────────────────┘

正确! 接下来我决定摆脱尽可能多的控制结构:

   gj1 =:dyad :0
    y=.y,.b 
    for_d.i.#y do.
     for_r.d ({.,]}.~[:>:[) i.#y do. NB. for indices without d
      t=.%/ (<"1(r,d),:(d,d)) { y
      y=.((r{y)-(t*d{y)) r}y NB. no need to iterate for each column
      y=.0 (<r,d)} y
     end. 
    end.
    ({:"1 y)%(+/}:"1 y) NB. b divide by sum of each item of A (drop zeroes)
   )
   b gj1 A
_1 3 _2 2

好的,现在我可以尝试翻译for_r. - 陷入隐性形式......但看起来它看起来会更加繁琐,我认为我的方式是错误的 - 但是什么是没有错误的研究? 我真的想把Gauss-Jordan方法默认编写为:

  • J编码练习
  • 看看它的表现是否更好
  • 几个星期后尝试理解代码:)

请帮我把它写到最后或指出一个更好的方法。


Task to code Gauss-Jordan method of solving linear system of algebraic equations is an exercise that I've selected to advance in learning J. System is Ax=b, where A is n-by-n matrix, b and unknown x are n-vectors. Firstly, I've started with the simplest form with control structures:

   gj0 =: dyad :0 NB. usage is same to %.
    y=.y,.b 
    for_d.i.#y do. 
     for_r.i.#y do. 
      if.r=d do.continue.end. NB. do not eliminate d'th row
      t=.%/ (<"1(r,d),:(d,d)) { y
      for_c.d}.>:i.#y do.
       y=.(((<r,c){y)-(t*(<d,c){y)) (<r,c)} y
      end.
      y=.0 (<r,d)} y NB. ensure zero
     end. 
    end. NB. now A is diagonal but not identity matrix, so:
    x=.{:"1 y NB. x = b
    for_r.i.#y do.
     x=.((r{x)%(<r,r){y) r} x NB. divide by coefficients on diagonal
    end. 
   )
   Ab =: (".;._2) 0 :0
   0.25 _0.16 _0.38  0.17
   0.19 _0.22 _0.02  0.41
   0.13  0.08 _0.08 _0.13
   0.13 _0.1  _0.32  0.65
   )
   b =: 0.37 0.01 0.01 1.51
   (,.".&.>)('A';'b';'gj0 A,.b';'b %. A')
┌────────┬──────────────────────┐
│A       │0.25 _0.16 _0.38  0.17│
│        │0.19 _0.22 _0.02  0.41│
│        │0.13  0.08 _0.08 _0.13│
│        │0.13  _0.1 _0.32  0.65│
├────────┼──────────────────────┤
│b       │0.37 0.01 0.01 1.51   │
├────────┼──────────────────────┤
│b gj0 A │_1 3 _2 2             │
├────────┼──────────────────────┤
│b %. A  │_1 3 _2 2             │
└────────┴──────────────────────┘

Correct! Next I've decided to get rid of as many control structures as possible:

   gj1 =:dyad :0
    y=.y,.b 
    for_d.i.#y do.
     for_r.d ({.,]}.~[:>:[) i.#y do. NB. for indices without d
      t=.%/ (<"1(r,d),:(d,d)) { y
      y=.((r{y)-(t*d{y)) r}y NB. no need to iterate for each column
      y=.0 (<r,d)} y
     end. 
    end.
    ({:"1 y)%(+/}:"1 y) NB. b divide by sum of each item of A (drop zeroes)
   )
   b gj1 A
_1 3 _2 2

OK, Now I can try to translate for_r.-loop into tacit form... but it seems like it will look more cumbersome and I think that I'm at a wrong way -- but what's study without mistakes? I really want to code Gauss-Jordan method tacitly to:

  • exercise in J coding
  • see if it is better in performance
  • try to understand code a few weeks later :)

Help me please write it to the end or point out a better approach.


原文:https://stackoverflow.com/questions/26317674
更新时间:2022-03-19 09:03

相关问答

更多
  • sudo launchctl load /Library/LaunchDaemons/parallel.plist 应该使守护程序自动运行。 请参阅http://www.aandcp.com/launchdaemons-and-mac-os-x-openvpn-as-an-example sudo launchctl load /Library/LaunchDaemons/parallel.plist should cause the daemon to autorun. See http://www. ...
  • 这个想法是,你可以分开应用程序的依赖关系; 试图使应用程序更加便携。 这个想法是,编译时可以使应用程序依赖于api.jar 。 然后当你想运行程序时,你可以切换到合适的实现jar( impl.jar )和合适的资源包jar( bundle.jar )。 作为一个例子,假设图书馆进行一些数据库交互。 您编写代码以便它引用api.jar 。 现在假设你需要它与特定类型的数据库(例如MySQL)一起工作 - 然后你可以将特定于MySQL数据库的impl.jar添加到类路径中以使其起作用(如果以后需要不同的数据库 ...
  • 我同意Mark的观点,您应该将文件放入本地存储库而不是缓存。 特别是因为预期缓存可能(并且经常)随时被删除。 但是,要解决您的问题,最可能的原因是您的文件夹层次结构与预期模式不匹配。 默认情况下,缓存布局如下: [organisation]/[module]/[revision]/[type]s/[artifact].[ext] 所以你必须将你的文件移动到以下目录以消除该错误: .ivy2\cache\apache.org\lucene\3.0.3\jars\lucene.jar 有时,默认模式会根据设 ...
  • 为了创建一个.jar文件,你需要使用jar而不是java : jar cf myJar.jar myClass.class 另外,如果要使其可执行,您需要为您的应用程序指定一个入口点 (即一个具有public static void main(String[] args) ))。 这通常是通过创建一个包含Main-Class头的清单文件(例如Main-Class: myClass )实现的。 但是,正如Mark Peters指出的那样,使用JDK 6,可以使用e选项定义入口点: jar cfe myJar ...
  • 使用IDE,它可以让生活变得更加轻松。 但是,如果你坚持以旧时尚的方式做事,请尝试关闭你的防病毒软件。 如果这不起作用,请检查以确保正确设置了环境变量。 如果所有其他方法都失败了,请尝试重新安装JDK。 Use an IDE, it makes life so much easier. But, if you insist on doing things the old fashion way, try turning off your antivirus. If that doesn't work, ch ...
  • 问题是您正在创建一个new FileOutputStream(tempName)来写入该文件,但从不关闭该输出流(或链接到它的另一个输出流)。 做这个: FileOutputStream fos = newFileOutputStream(tempName); // use it fos.close(); // CLOSE IT!! // then you can delete the file 简化 也许你可以用另一种方式完成工作,没有临时文件...... 通过示例: doc.write(new Fi ...
  • 不 java -cp mewLog_macosx-x86.jar mewlog.MLMain 工作? 如果是这样,那么Jar中的清单文件指向错误的Main-Class(不考虑包中) does java -cp mewLog_macosx-x86.jar mewlog.MLMain work? if so then your manifest file in the Jar is pointing to the wrong Main-Class (Not taking the package into acc ...
  • 重命名14compatibility.jar后我没有任何问题。 也许你可以尝试这样做。 如果有什么可怕的事情发生,你可以将它移回原来的位置。 I haven't had any problems after renaming 14compatibility.jar. Perhaps you could try doing that. If anything breaks horribly, you could move it back in its original place.
  • 您可以使用现有的启动配置在导出向导选择对话框中(右键单击项目并转到导出...)在Eclipse 3.4+中创建一个Runnable JAR ,该配置将包含库或重新打包它们。 配置文件应该与runnable jar所在的目录相同。 如果您需要任何帮助来加载这些,只需要问:) alt text http://download.eclipse.org/eclipse/downloads/drops/R-3.5-200906111540/images/runnable-jar-in-jar-export.png Y ...
  • 我认为你应该在Bundle-NativeCode头中使用单个空格而不是制表符或多个空格作为JAR文件规范,名称 - 值对和章节指定: header: name : value name: alphanum *headerchar value: SPACE *otherchar newline *continuation continuation: SPACE *otherchar newline 这是一个适用于我的示例man ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。