covariance

Possible to convert IQueryable<Derived> to IQueryable<Base>?

戏子无情 提交于 2019-12-01 09:19:46
问题 I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Where<>() call? My use case is that I am trying to deal with a database schema that has many similar tables. Most of the fields are in common, and many of the common

.NET equivalent for Java wildcard generics <?> with co- and contra- variance?

▼魔方 西西 提交于 2019-12-01 07:35:21
I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. For instance: Java: interface IInterf { } class Impl implements IInterf { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); void method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val); } abstract class Generic<T extends Impl> implements IGeneric1<T>, IGeneric2<T> { public void method1(IGeneric2<?> val2) { val2.method2(this); } }

C# Type Conversion Error Despite Generic Constraint

半腔热情 提交于 2019-12-01 06:02:13
问题 Why, with a generic constraint on type parameter T of class P of "must inherit from A", does the first call succeed but the second call fail with the type conversion error detailed in the comment: abstract class A { } static class S { public static void DoFirst(A argument) { } public static void DoSecond(ICollection<A> argument) { } } static class P<T> where T : A, new() { static void Do() { S.DoFirst(new T()); // this call is OK S.DoSecond(new List<T>()); // this call won't compile with: /*

Covariance and Contravariance with C# Arrays [duplicate]

房东的猫 提交于 2019-12-01 05:26:35
This question already has an answer here: Covariance and contravariance real world example 8 answers While reading a section of an article about covariance and contravariance at Wikipedia, I ran into the following, bolded sentence: First consider the array type constructor: from the type Animal we can make the type Animal[] ("array of animals"). Should we treat this as Covariant: a Cat[] is a Animal[] Contravariant: a Animal[] is a Cat[] or neither (invariant)? If we wish to avoid type errors, and the array supports both reading and writing elements, then only the third choice is safe. Clearly

Numpy Cholesky decomposition LinAlgError

孤者浪人 提交于 2019-12-01 04:08:57
In my attempt to perform cholesky decomposition on a variance-covariance matrix for a 2D array of periodic boundary condition, under certain parameter combinations, I always get LinAlgError: Matrix is not positive definite - Cholesky decomposition cannot be computed . Not sure if it's a numpy.linalg or implementation issue, as the script is straightforward: sigma = 3. U = 4 def FromListToGrid(l_): i = np.floor(l_/U) j = l_ - i*U return np.array((i,j)) Ulist = range(U**2) Cov = [] for l in Ulist: di = np.array([np.abs(FromListToGrid(l)[0]-FromListToGrid(i)[0]) for i, x in enumerate(Ulist)]) di

PyMC - variance-covariance matrix estimation

匆匆过客 提交于 2019-12-01 01:59:35
I read the following paper( http://www3.stat.sinica.edu.tw/statistica/oldpdf/A10n416.pdf ) where they model the variance-covariance matrix Σ as: Σ = diag(S)*R*diag(S) (Equation 1 in the paper) S is the k×1 vector of standard deviations, diag(S) is the diagonal matrix with diagonal elements S, and R is the k×k correlation matrix. How can I implement this using PyMC ? Here is some initial code I wrote: import numpy as np import pandas as pd import pymc as pm k=3 prior_mu=np.ones(k) prior_var=np.eye(k) prior_corr=np.eye(k) prior_cov=prior_var*prior_corr*prior_var post_mu = pm.Normal("returns"

Covariance and Contravariance with C# Arrays [duplicate]

喜夏-厌秋 提交于 2019-12-01 01:21:40
问题 This question already has answers here : Covariance and contravariance real world example (8 answers) Closed 6 years ago . While reading a section of an article about covariance and contravariance at Wikipedia, I ran into the following, bolded sentence: First consider the array type constructor: from the type Animal we can make the type Animal[] ("array of animals"). Should we treat this as Covariant: a Cat[] is a Animal[] Contravariant: a Animal[] is a Cat[] or neither (invariant)? If we

Derived type of generic base class

天涯浪子 提交于 2019-11-30 23:31:19
I have the following code. class Header<T> where T: IItem { } class HeaderA : Header<ItemA> { } class HeaderB : Header<ItemB> { } interface IItem { } class ItemA : IItem { } class ItemB : IItem { } Header<IItem> h = new HeaderA(); The last line cannot be compiled. Cannot implicitly convert type 'UserQuery.HeaderA' to 'UserQuery.Header<UserQuery.IItem>' HeaderA is a subtype of Header and ItemA is a subtype of IItem. Why it doesn't work? KeithS In short, you're trying to use a concept called covariance , which is not supported in .NET generic classes , and not supported by default in interfaces

Covariance Matrix of an Ellipse

微笑、不失礼 提交于 2019-11-30 20:41:19
I've been trying to solve a problem. I'm surprised I haven't been able to find anything really useful on the net. I know that from the eigenvalues of the covariance matrix of the ellipse, the major and the minor axis of the ellipse can be computed. As the following: a1 = 2*sqrt(e1) a2 = 2*sqrt(e2) where a1 and a2 are the major and minor axis, e1 and e2 are the eigenvalues of covariance matrix. My question is: given an edge points (xi,yi) of the image ellipse, how it possible to find the 2×2 covariance matrix of that ellipse? Just by pure reverse engineering (I'm not familiar anymore with this

Interfaces and covariance problem

孤街浪徒 提交于 2019-11-30 20:14:59
I have a particular class that stores a piece of data, which implements an interface: template<typename T> class MyContainer : public Container<T> { class Something : public IInterface { public: // implement *, ->, and ++ here but how? private: T x; }; // implement begin and end here, but how? private: Something* data; // data holds the array of Somethings so that references to them can be returned from begin() and end() to items in this array so the interface will work, but this makes the problem described below }; And I have an array of Something s. I have the need for Something to implement