[TestMethod]
public void DescartesTest()
{
List<List<float>> input = new List<List<float>>()
{
new List<float>(){1f,2f},
new List<float>(){2f,3f}
};
var descat = Descartes(input);
List<List<float>> expert = new List<List<float>>()
{
new List<float>(){2f,1f},
new List<float>(){3f,1f},
new List<float>(){2f,2f},
new List<float>(){3f,2f}
};
for (int i = 0; i < expert.Count; i++)
{
Assert.IsTrue(expert[i].SequenceEqual(descat[i]));
}
}
//生成笛卡尔积
public static List<List<float>> Descartes(List<List<float>> input)
{
if (input.Count() == 1)
{
List<List<float>> result = new List<List<float>>();
for (int i = 0; i < input[0].Count; i++)
{
result.Add(new List<float>() { input[0][i] });
}
return result;
}
var items = from i in input.ElementAt(0)
from j in Descartes(input.Skip(1).ToList())
select j.Append(i).ToList();
return items.ToList();
}
04-16
08-15
843