洛伦(Matlab)的艺术

将想法变成MATLAB

Brief History of Nonnegative Least Squares in MATLAB

在我在Mathworks(1987!)的第一年,我认识的教授与我取得了联系。他试图通过非阴性约束来解决最小二乘问题。正确提高后,我立即知道从哪里获得出色的算法

  • Lawson和Hanson,“解决最小二乘问题”,Prentice-Hall,1974年,第23章,第2页。161。

内容

Fortran Code

书中提供了《福特守则》,尽管Matlab还没有statement. Problem was, it wasn't working correctly. He asked me to take a look. I remember being excited, because I lovennlsas L-H call the algorithm. However, pouring over someone else's translated Fortran loses its pizzazz quite quickly.

让我给您一些有关Fortran代码本身的统计信息。

  • 326lines total
  • ~150 lines of comments
  • leaving ~175 lines of executable code
  • main double while-loop, ~125 lines of executable code (lines 83-289)

A Eureka Moment

Procrastination can pay off sometimes. Instead of reading through the M-file code, comparing it to the Fortran line-by-line, I opened up the reference for the algorithm. Here's a glimpse of what I found.

你看见我看到的了吗?我看到问题在矩阵和向量方面谈论。嗯,也许我可以直接从算法描述中实现它,绕过Fortran实现。所以我做到了。

The MATLAB Code

MATLAB运送了功能nnls,最终被重命名为LSQNONNEGand was updated to include some code to standardize it with our other optimization routines. I had the code written and running in less than half a day, including working correctly on the examples sent to me by the professor that were not working in his translated code.

The code statistics look like this for the M-file.

  • 总计206行
  • 〜80空白和评论行
  • ~125 lines of executable code, of which ~55 dealing with the infrastructure common to our other optimization routines that don't appear in the Fortran
  • ~50 lines for the main iteration loops (nested while-loops)

Lessons Learned

  • 我从这次经历中重新确定,值得回去思考代码和算法,然后才能潜水。我想我必须在我的生活中学习几次!虽然M-Files的Fortran和两个版本都是基于同一算法的,但我能够使用参考的矩阵符号绕过编写的代码并编写版本。
  • MATLAB代码较短,尽管我敢肯定有人会说不是that much短。
  • 当我编写它时,该算法确实为我沉没了,因为我没有被循环陷入困境,所以我看着变量进出活跃的集合。看到算法从不太细微的水平上起作用,但仍在捕捉,这是非常有趣的全部细节。

Care to share any of your experiences where thinking about an algorithm differently than the way it was posed help you out? Add your thoughtshere




以MATLAB®7.3出版

|

评论

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.