本文整理汇总了C#中IEnumerable.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# IEnumerable.Sum方法的具体用法?C# IEnumerable.Sum怎么用?C# IEnumerable.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnumerable
的用法示例。
在下文中一共展示了IEnumerable.Sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Pearson
public static double Pearson(IEnumerable<long> v1, IEnumerable<long> v2)
{
var sum1 = v1.Sum();
var sum2 = v2.Sum();
// Sum of the squares
var sum1Sq = v1.Sum(v => m.Pow(v, 2));
var sum2Sq = v2.Sum(v => m.Pow(v, 2));
// Sum of the products
var l1 = v1.ToArray();
var l2 = v2.ToArray();
if (l1.Length != l2.Length)
throw new ApplicationException("Input data should contain the equal number of items");
long pSum = Enumerable.Range(0, l1.Length)
.Select(i => l1[i] * l2[i])
.Sum();
// Calculate r (Pearson score)
var num = pSum - (sum1 * sum2 / l1.Length);
var den =
m.Sqrt((sum1Sq - m.Pow(sum1, 2)/l1.Length)*(sum2Sq - m.Pow(sum2, 2)/l2.Length));
if (den == 0)
return 0;
return 1.0 - num /den;
}
示例2: Run
public Run(IEnumerable<Assembly> assemblies) : base(assemblies.Cast<SpecificationContainer>())
{
_assemblies = assemblies;
_totalAssemblies = assemblies.Count();
_totalConcerns = assemblies.Sum(x => x.TotalConcerns);
_totalContexts = assemblies.Sum(x => x.TotalContexts);
}
示例3: Report
public void Report(IEnumerable<SuiteRunResult> executed)
{
var totalPasses = executed.Sum(x => x.Passes);
var totalFailures = executed.Sum(x => x.Failures);
var total = totalPasses + totalFailures;
_output.WriteLine("Loaded: {0}".FormatWith(executed.Select(s => s.Name).Join(", ")));
_output.WriteLine(" {0} {1} loaded.".FormatWith(
total,
"test".Pluralize(total)));
_output.WriteLine(" {0}/{1} {2} passed ({3} {4}).".FormatWith(
totalPasses,
total,
"test".Pluralize(total),
totalFailures,
"failure".Pluralize(totalFailures)));
if (totalFailures > 0)
_output.WriteLine("Failures: ");
executed.Each(suiteResult => suiteResult
.Results
.Where(containerResult => containerResult.Results.Any(r => !r.Pass))
.Each(f =>
{
_output.WriteLine(" {0}.{1} contained {2} failures.".FormatWith(suiteResult.Name, f.Name, f.Failures));
f.Results.Where(r => !r.Pass).Each(r => _output.WriteLine(" {0} failed. [{1}]".FormatWith(r.Name, r.Message)));
}));
_output.Flush();
}
示例4: SpecificationContainer
protected SpecificationContainer(IEnumerable<SpecificationContainer> specificationContainers)
{
_totalSpecifications = specificationContainers.Sum(x => x.TotalSpecifications);
_passingSpecifications = specificationContainers.Sum(x => x.PassingSpecifications);
_failingSpecifications = specificationContainers.Sum(x => x.FailingSpecifications);
_notImplementedSpecifications = specificationContainers.Sum(x => x.NotImplementedSpecifications);
}
示例5: Run
public Run(IEnumerable<Assembly> assemblies) : base(assemblies.Cast<SpecificationContainer>())
{
Meta = new Meta { GeneratedAt = DateTime.Now };
_assemblies = assemblies.OrderBy(x => x.Name);
_totalAssemblies = assemblies.Count();
_totalConcerns = assemblies.Sum(x => x.TotalConcerns);
_totalContexts = assemblies.Sum(x => x.TotalContexts);
}
示例6: RiskResult
public RiskResult(IEnumerable<StationPath> path, double radius)
{
Radius = radius;
Path = path;
TotalRisk = path.Sum(x => x.Risk);
TotalDistance = path.Sum(x => x.Distance);
TotalDistanceRatio = TotalDistance / (Math.PI * radius);
}
示例7: getAverageColor
private static CieLabColor getAverageColor(IEnumerable<CieLabColor> colors)
{
double colorCount = colors.Count();
double l = colors.Sum(color => color.L) / colorCount;
double a = colors.Sum(color => color.A) / colorCount;
double b = colors.Sum(color => color.B) / colorCount;
return new CieLabColor((float)l, (float)a, (float)b);
}
示例8: WeightingDensity
public double WeightingDensity(IEnumerable<MaterialDensityCalculationItem> items)
{
//错误输入验证代码后续
double[] tmpValue = new double[items.Count()];
double sum = items.Sum(i => i.MoleWeight * i.At);
double weightingDensity = items.Sum(i => i.At * i.MoleWeight / sum * i.Density);
return weightingDensity;
}
示例9: SlopeOfPoints
/// <summary>
/// Gets the slope for a set of points using the formula:
/// m = SUM(x-AVG(x)(y-AVG(y)) / SUM(x-AVG(x))^2
/// </summary>
/// <param name="points">Points to calculate the Slope from</param>
/// <returns>SlopeOfPoints</returns>
private static float SlopeOfPoints(IEnumerable<PointF> points)
{
float avgX = points.Average(p => p.X);
float avgY = points.Average(p => p.Y);
float dividend = points.Sum(p => (p.X - avgX) * (p.Y - avgY));
float divisor = (float)points.Sum(p => Math.Pow(p.X - avgX, 2));
return dividend / divisor;
}
示例10: CardStatsByRarity
public CardStatsByRarity(string rarity, IEnumerable<CardInCollection> cards)
{
Rarity = rarity;
TotalAmount = cards.Select(c => c.MaxAmountInCollection)
.Sum();
PlayerHas = cards.Sum(c => c.AmountNonGolden);
PlayerHasGolden = cards.Sum(c => c.AmountGolden);
OpenGoldenOdds = CalculateOpeningOdds(cards, card => card.MaxAmountInCollection - card.AmountGolden, GoldenCardProbabilities);
OpenNonGoldenOdds = CalculateOpeningOdds(cards, card => card.MaxAmountInCollection - card.AmountNonGolden, CardProbabilities);
}
示例11: BenchmarkResultList
public BenchmarkResultList(IEnumerable<BenchmarkResult> results)
{
_results = results.ToArray();
var totalTicks = _results.Sum(r => r.Time.Ticks);
_totalTime = TimeSpan.FromTicks(totalTicks);
_totalIterations = _results.Sum(r => r.Iterations);
if (_totalIterations > 0)
_average = TimeSpan.FromTicks(totalTicks/_totalIterations);
}
示例12: Order
public Order(IEnumerable<Outcome> outcomes, List<OrderLine> orderLines)
: this()
{
Lines = orderLines;
orderLines.ForEach(ol => ol.Order = this);
Summ = outcomes.Sum(o => o.Summ);
SummWithDiscount = outcomes.Sum(o => o.SummWithDiscount);
Profit = outcomes.Sum(o => o.Profit);
DiscountSumm = outcomes.Sum(o => o.DiscountSumm);
}
示例13: InterfaceDefinitionsGenerator
public InterfaceDefinitionsGenerator(IEnumerable<TypeScriptModule> modules, GeneratorOptions options)
{
_modules = modules;
_sb = new IndentedStringBuilder(modules.Sum(m => m.ModuleMembers.Count) * 256, options.CodeGenerationOptions.IndentationCharacter, options.CodeGenerationOptions.IndentationIncrementAmount);
_options = options;
_propertyCommenter = new PropertyCommenter(options);
}
示例14: CalculatePrice
public Decimal CalculatePrice(IEnumerable<PotterBook> products)
{
Decimal summaryPrice;
var bookCount = products.Count();
summaryPrice = products.Sum(x => x.Price);
switch (bookCount)
{
case 2:
summaryPrice = summaryPrice * 0.95m;
break;
case 3:
summaryPrice = summaryPrice * 0.9m;
break;
case 4:
summaryPrice = summaryPrice * 0.8m;
break;
case 5:
summaryPrice = summaryPrice * 0.75m;
break;
case 1:
default:
break;
}
return summaryPrice;
}
示例15: HarmonicMean
/// <summary>
/// Calculates the harmonic mean of the given numbers.
/// The harmonic mean is defined as zero if at least one of the numbers is zero.
/// </summary>
/// <param name="numbers">The numbers whose harmonic mean is to be calculated.</param>
/// <returns>The harmonic mean of the given numbers.</returns>
/// <exception cref="System.InvalidOperationException">
/// The specified collection must not contain negative numbers and must not be empty.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// The specified collection must not be null.
/// </exception>
public static double HarmonicMean(IEnumerable<double> numbers)
{
if (numbers == null)
{
throw ArgumentNullException;
}
if (!numbers.Any())
{
throw EmptyNumbersCollectionException;
}
if (numbers.Any(number => number < 0))
{
throw CollectionContainingNegativeNumbersException;
}
if (numbers.Contains(0))
{
// If one of the values strives against zero the limiting value of the harmonic mean does, too.
// Therefore, it is sensible to define the harmonic mean as being zero if at least one of the values is zero.
return 0;
}
double sumOfReciprocalValues = numbers.Sum(number => 1.0 / number);
double harmonicMean = numbers.Count() / sumOfReciprocalValues;
return harmonicMean;
}