如何自动化代码的修改?

发布于 2025-02-09 00:38:08 字数 409 浏览 2 评论 0原文

我有大量的代码,包括数千个方程式。两个结缔线的示例,与第998和999行相对应的示例

sum(x[i]*y[i]for i in 1:n) == 15;
sum(z[i]*x[i]for i in 1:n) == 30;

我想用以下

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 <=  s[998];

sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 <=  s[999];

如何自动化此过程来替换此类行?

I have a large piece of code consisting of thousands of equations. Example of two connective lines, corresponding to lines 998 and 999 is

sum(x[i]*y[i]for i in 1:n) == 15;
sum(z[i]*x[i]for i in 1:n) == 30;

I would like to replace such lines with the following

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 <=  s[998];

sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 <=  s[999];

How to automate this process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2025-02-16 00:38:08

替换

我们的第一步是转换每条线:

sum(x[i]*y[i]for i in 1:n) == 15;

向此:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];

虽然在一个命令中进行操作将是一个好的客厅技巧,但我们将在几个更容易地遵循步骤中进行操作。

  1. == to -

    这是我们的第一个命令,非常简单:

     :%s/==/ - &&lt; cr&gt;
     
    • 输入命令行模式。
    • 是执行以下命令的行范围。在这里,1,$(从第1行到最后一行)的快捷方式,因此“每行”。
    • s是“替代”命令,请参见:help:s
    • /==是您要替换的。
    • / -是您要用它代替的。
    • &lt; cr&gt;(enter)执行命令。

    简单的英语:“用-替换==”。

    我们应该得到类似的东西:

      sum(x [i]*y [i] for 1:n)-15;
    sum(z [i]*x [i]对于1:n)-30;
     

  2. ; to &gt; = -s [xxx];

    这是我们的第二个命令:

     :%s/;/&gt; =  -  s [xxx];
     

    我们将每个;替换为通用&gt; = -s [xxx];。这也很简单。

    简单英语:“用;&gt; = -s [xxx];。。

    我们应该得到类似的东西:

      sum(x [i]*y [i] for 1:n)-15&gt; = -s [xxx];
    sum(z [i]*x [i] for 1:n)-30&gt; =  -  s [xxx];
     
  3. xxx到行号

    这是我们的第三个命令:

     :%s/xxx/\ = line('。')
     

    此命令与其他命令之间的重大变化是,替换部分是动态的。使用\ =,我们使用每个执行过程中评估的表达式,而不是固定的字符串。 line('。')是一个返回行号的vimscript函数,这正是我们在这些方括号之间想要的。

    简单的英语:“用当前行号替换xxx

    我们应该得到类似的东西:

      sum(x [i]*y [i] for 1:n)-15&gt; = -s [998];
    sum(z [i]*x [i] for 1:n)-30&gt; =  -  s [999];
     

在这里复制

,我们将每行重复一个命令:

:g/^/t.|s/-s/ s
  • :g:help:help:global命令。
  • /^/匹配每一行,因此每行将执行以下命令。
  • t:help:t命令。
  • 表示当前行。
  • |分开两个命令。
  • s/-s/s在重复的行上的s之前,请删除-

用简单的英语:“标记每条行,然后在自身下方复制每个标记的行,然后在s之前删除该领先的-”。

我们应该得到类似的东西:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 >=  s[998];
sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 >=  s[999];

最后一件事

我们使用最后一个命令在我们的块之间添加一条线:

:g/ s[/put=''
  • 每个行匹配s [都标记为标记。
  • 我们使用:帮助:放置以附加空行。

用简单的英语:“在每行之后,用s [>“放一条空行”。

我们应该得到类似的东西:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 >=  s[998];

sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 >=  s[999];

我们学到了什么?

  • 如何用执行简单的替代:help:替换
  • 的概念:help:range
  • 如何在替换部分中使用表达式(通常的网关药物来vimscript) 重新定位 - \ = ,
  • 如何在与匹配特定模式的行上执行任意命令
  • :帮助子 的地址:help:t
  • 当前行下方的空字符串,在此处为空字符串。
  • 如何在当前行下方使用:help:put,如何用分开命令的 :help:|

Substitutions

Our first step is to transform each individual line:

sum(x[i]*y[i]for i in 1:n) == 15;

into this:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];

While doing it in one command would be a nice parlor trick, we will do it in several easier to follow steps.

  1. == to -

    Here is our first command, it is very simple:

    :%s/==/-<CR>
    
    • : enters command-line mode.
    • % is the range of lines on which to execute the following command. Here, % is a shortcut for 1,$ (from line 1 to last line), thus "every line".
    • s is the "substitute" command, see :help :s.
    • /== is what you want to substitute.
    • /- is what you want to substitute it with.
    • Press <CR> (ENTER) to execute the command.

    In plain english: "substitute every == with -".

    We should get something like:

    sum(x[i]*y[i]for i in 1:n) - 15;
    sum(z[i]*x[i]for i in 1:n) - 30;
    
  2. ; to >= -s[XXX];

    Here is our second command:

    :%s/;/ >= -s[XXX];
    

    Where we substitute every ; with a generic >= -s[XXX];. This is also quite simple.

    In plain english: "substitute every ; with >= -s[XXX];.

    We should get something like:

    sum(x[i]*y[i]for i in 1:n) - 15 >= -s[XXX];
    sum(z[i]*x[i]for i in 1:n) - 30 >= -s[XXX];
    
  3. XXX to line number

    Here is our third command:

    :%s/XXX/\=line('.')
    

    The big change between this command and the other ones is that the replacement part is dynamic. With \=, we use an expression that is evaluated during each execution instead of a fixed string. line('.') is a vimscript function that returns a line number, which is exactly what we want between those brackets.

    In plain english: "substitute every XXX with the current line number".

    We should get something like:

    sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
    sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
    

Duplication

Here we duplicate each line with a single command:

:g/^/t.|s/-s/ s
  • :g is the :help :global command.
  • /^/ matches every line so the following command will be executed on every line.
  • t is the :help :t command.
  • . represents the current line.
  • | separates two commands.
  • s/-s/ s removes the - before the s on the duplicated line.

In plain english: "mark every line, then copy each marked line below itself, then remove that leading - before the s".

We should get something like:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 >=  s[998];
sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 >=  s[999];

One last thing

We use one last command to add a line between our blocks:

:g/ s[/put=''
  • Every line matching s[ is marked.
  • We use :help :put to append an empty line.

In plain english: "put an empty line after each line with s[".

We should get something like:

sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 >=  s[998];

sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 >=  s[999];

What did we learn?

  • how to perform simple substitutions with :help :substitute,
  • the notion of :help :range,
  • how to use an expression in the replacement part (the usual gateway drug to vimscript) with :help sub-replace-\=,
  • how to execute arbitrary commands on lines matching a specific pattern with :help :global,
  • how to copy a given line to a given address with :help :t,
  • how to put some text, here an empty string, below the current line with :help :put,
  • how to separate commands with :help :|.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文