对于我最近对矩阵操作项目提出的问题,我可以确认每件事情都在工作,perfectly...except是某个特定的操作。当然,我说的是一个函数来检查矩阵是否被简化为行梯队形式,作为函数/逻辑编程的新手,这确实是我所面临的最困难的任务。
因此,对于这个函数,我需要检查:
如果从左到右对角线没有零,则为
这一次,我实际上是在寻求全面的帮助,因为我甚至不知道如何开始制造这种混乱,我真的希望有一个慷慨的灵魂来执行这个雄心勃勃的功能,并请解释她的思维方式和程序。
发布于 2022-06-01 14:31:35
我以前没有听说过行梯队的形式,我找到了https://stattrek.com/matrix-algebra/echelon-form.aspx,它告诉我,只要每一行“开始”比前面的行“开始”,就可以在从左到右的对角线中出现零。所以我在想:
- Manual recursion grind, if the start of the list is a zero then increment a counter, and recurse down the remaining elements. If the start is not a zero, stop counting.- Or if the row is all zeros.
- and if this ratchet/all zeros holds for remaining rows.% the number of leading zeros in a list [0,0,1,2,3] -> 2
row_leadZeroCount([], LZC, LZC).
row_leadZeroCount([N|Ns], ZerosToHere, LZC) :-
(N=0 -> (ZerosSeen is 1+ZerosToHere,
row_leadZeroCount(Ns, ZerosSeen, LZC))
; LZC = ZerosToHere). % stop counting at the first non-zero.
% Nested lists are in Row Echelon Form if
% the leading zeros in this row are more than the previous row
% or we're at a state of all remaining rows being only zeros.
matrix_ref_([], _, _).
matrix_ref_([Row|Rows], RowLen, Ratchet) :-
row_leadZeroCount(Row, 0, RowZeros),
(RowZeros < RowLen -> RowZeros > Ratchet
; RowZeros = RowLen),
matrix_ref_(Rows, RowLen, RowZeros).
% wrapper to get row length, to see when a row is all zeros,
% and start the row length Ratchet going.
matrix_ref([Row|Rows]) :-
length(Row, RowLen),
matrix_ref_([Row|Rows], RowLen, -1).例如:
?- _Mx = [[1,2,3,4],
[0,0,1,3],
[0,0,0,1], %Example from linked web page
[0,0,0,0]],
matrix_ref(_Mx).
true注意:你说的是“归为行梯队形式”;我忽略了链接中的“简化行梯队形式”,假设这不是你想要的。
https://stackoverflow.com/questions/72455732
复制相似问题