Created
October 24, 2012 20:20
-
-
Save wcharczuk/3948606 to your computer and use it in GitHub Desktop.
Implementation of LogSpace and LinSpace from Numpy for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace libSVM | |
{ | |
public static class Numpy | |
{ | |
public static IEnumerable<double> Arange(double start, int count) | |
{ | |
return Enumerable.Range((int)start, count).Select(v => (double)v); | |
} | |
public static IEnumerable<double> Power(IEnumerable<double> exponents, double baseValue = 10.0d) | |
{ | |
return exponents.Select(v => Math.Pow(baseValue, v)); | |
} | |
public static IEnumerable<double> LinSpace(double start, double stop, int num, bool endpoint = true) | |
{ | |
var result = new List<double>(); | |
if (num <= 0) | |
{ | |
return result; | |
} | |
if (endpoint) | |
{ | |
if (num == 1) | |
{ | |
return new List<double>() { start }; | |
} | |
var step = (stop - start)/ ((double)num - 1.0d); | |
result = Arange(0, num).Select(v => (v * step) + start).ToList(); | |
} | |
else | |
{ | |
var step = (stop - start) / (double)num; | |
result = Arange(0, num).Select(v => (v * step) + start).ToList(); | |
} | |
return result; | |
} | |
public static IEnumerable<double> LogSpace(double start, double stop, int num, bool endpoint = true, double numericBase = 10.0d) | |
{ | |
var y = LinSpace(start, stop, num:num, endpoint:endpoint); | |
return Power(y, numericBase); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment