Kaynağa Gözat

Seemingly working Gaussian elimination

Yentl Van Tendeloo 8 yıl önce
ebeveyn
işleme
de69281135
1 değiştirilmiş dosya ile 31 ekleme ve 15 silme
  1. 31 15
      integration/code/cbd_semantics.alc

+ 31 - 15
integration/code/cbd_semantics.alc

@@ -7,13 +7,6 @@ include "io.alh"
 include "metamodels.alh"
 include "compilation_manager.alh"
 
-Void function main():
-	Element model
-	String verify_result
-
-	while (True):
-		execute_cbd(instantiate_model(import_node("models/CausalBlockDiagrams_Design")))
-
 Element function retype_to_runtime(design_model : Element):
 	Element runtime_model
 	Element all_blocks
@@ -446,6 +439,9 @@ Void function execute_cbd(design_model : Element):
 		else:
 			log("Did not understand command: " + cmd)
 
+Float function v2f(i : Element):
+	return cast_s2f(cast_v2s(i))!
+
 Void function eliminateGaussJordan(m : Element):
 	Integer i
 	Integer j
@@ -459,8 +455,10 @@ Void function eliminateGaussJordan(m : Element):
 	j = 0
 
 	while (i < read_nr_out(m)):
+		log("Iteration " + cast_v2s(i))
+		log(matrix2string(m))
 		// Make sure pivot m[i][j] != 0, swapping if necessary
-		while (m[i][j] == 0):
+		while (v2f(m[i][j]) == 0.0):
 			// Is zero, so find row which is not zero
 			f = i + 1
 			searching = True
@@ -470,37 +468,45 @@ Void function eliminateGaussJordan(m : Element):
 					searching = False
 					j = j + 1
 				else:
-					if (m[f][j] == 0):
+					if (v2f(m[f][j]) == 0.0):
 						// Also zero, so continue
 						f = f + 1
 					else:
 						// Found non-zero, so swap row
-						t = m[f]
-						dict_overwrite(m, f, m[i])
+						t = v2f(m[f])
+						dict_overwrite(m, f, v2f(m[i]))
 						dict_overwrite(m, i, t)
 						searching = False
 			// If we have increased j, we will just start the loop again (possibly), as m[i][j] might be zero again
 
 		// Pivot in m[i][j] guaranteed to not be 0
 		// Now divide complete row by value of m[i][j] to make it equal 1
+		log("Pivot OK")
+		log(matrix2string(m))
 		f = j
-		divisor = m[i][j]
+		divisor = v2f(m[i][j])
 		while (f < read_nr_out(m[i])):
-			dict_overwrite(m[i], f, float_division(m[i][f], divisor))
+			dict_overwrite(m[i], f, float_division(v2f(m[i][f]), divisor))
 			f = f + 1
 
 		// Eliminate all rows in the j-th column, except the i-th row
+		log("Eliminate")
+		log(matrix2string(m))
 		f = 0
 		while (f < read_nr_out(m)):
 			if (bool_not(f == i)):
 				g = j
-				divisor = m[f][j]
+				divisor = v2f(m[f][j])
 				while (g < read_nr_out(m[f])):
-					dict_overwrite(m[f], g, m[f][g] - (divisor * m[i][g]))
+					dict_overwrite(m[f], g, v2f(m[f][g]) - (divisor * v2f(m[i][g])))
+					g = g + 1
+			f = f + 1
 
 		// Increase row and column
 		i = i + 1
 		j = j + 1
+		log("Increase")
+		log(matrix2string(m))
 
 	return !
 
@@ -516,6 +522,8 @@ String function matrix2string(m : Element):
 		while (j < read_nr_out(m[i])):
 			result = result + cast_v2s(m[i][j])
 			result = result + ", "
+			j = j + 1
+		i = i + 1
 		result = result + "\n"
 	return result!
 
@@ -530,6 +538,7 @@ Void function main():
 	list_append(t, -4)
 	list_append(m, t)
 
+	t = create_node()
 	list_append(t, 1)
 	list_append(t, 1)
 	list_append(t, 0)
@@ -542,3 +551,10 @@ Void function main():
 	log(matrix2string(m))
 
 	return !
+
+//Void function main():
+//	Element model
+//	String verify_result
+//
+//	while (True):
+//		execute_cbd(instantiate_model(import_node("models/CausalBlockDiagrams_Design")))