determinant algorithm of a 4x4 matrix - objective-c

I pick the first row and multiply each element by its cofactor,
but in some cases the method is returning nan.
For example,
1 0 0 1
0 2 0 0
0 0 3 0
0 0 0 4
in this case the method returns nan.
Does anyone know what I did wrong?
getDet3 returns determinant of a 3x3 matrix and it works fine.
-(double) getDet4:(double[4][4])mat {
double det = 0;
double small[3][3];
int i, j, k;
int i_ = 1, j_;
for ( i=0; i<4; i++ ){
if (mat[0][i] == 0) continue;
// get the small matrix here
for ( j=0; j<3; j++ ){
j_ = 0;
for ( k=0; k<3; k++ ){
if ( i == j_ ) j_++;
small[j][k] = mat[i_][j_];
j_++;
}
i_++;
}
det += mat[0][i] * [self getDet3:small] * pow(-1, i+j);
}
return det;
}

Well, there are a few mistakes in your code.
1) The initialization of i_ = 1 should be done just before the j loop, otherwise it will keep the old value.
2) The computation of pow(-1, i+j) should only depend on i, since j has the same value every time in that expression (namely, 3).
So, assuming that getDet3 is correct, the mistake is introduced by i_ going out of bounds. As a whole, the code should look like:
-(double) getDet4:(double[4][4])mat {
double det = 0;
double small[3][3];
int i, j, k;
int i_, j_;
for ( i=0; i<4; i++ ){
if (mat[0][i] == 0) continue;
// get the small matrix here
i_ = 1;
for ( j=0; j<3; j++ ){
j_ = 0;
for ( k=0; k<3; k++ ){
if ( i == j_ ) j_++;
small[j][k] = mat[i_][j_];
j_++;
}
i_++;
}
det += mat[0][i] * [self getDet3:small] * pow(-1, i);
}
return det;
}

Personally, I find your variable names confusing. If I understand your idea correctly, you expect i_ to have the value j + 1 and j_ to be k < i ? k : k + 1. IMHO, it would have been less confusing to have named them j_p andk_`, or even to just use the equivalent expression.
In any event, you don't reinitialize i_ inside the outer for loop. So it actually just keeps on incrementing, resulting in array indices outside of the array bounds.

Related

Did i calculate the Big O for these functions correctly?

I tried to find the time complexity of the following two functions:
the first one
public static int myMethod1(int[] arr) {
int x = 0;
for (int i = 0; i < arr.length / 2; i++) {
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr.length; k++) {
x++;
if (k == arr.length / 2) {
break;
}
}
}
}
return x;
}
So with this one i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k…
i and j, and k are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with three N’s.., which gives is O(N^3)
The next one is:
public static int myMethod(int N) {
int x = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N / 2; j++) {
for (int k = 1; k < N;) {
x++;
k *= 2;
}
}
}
return x;
}
With this i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k… i and j are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with two N’s.. The last loop k doubles, which gives is log(n).
The result of the this problem is therefore O(N^2· log (N))
is this correct? and if it is not, why?
You are right. In both of the questions

combine assimp bone data with other vertex data

My problem is that I am trying to load in joint/bone data from an fbx file in Direct X c++ using Assimp, but I want to store the eights and indices inside the same vertex struct that I store position, uv, etc.
I can make a loop for every vertex, but I also want to make a loop over each bone.
That means I can't have both the joint data and the other data in the same loop.
Should I create multilpe vertec objects then combine them afterwards?
I also am not sure how to find the bone ID and the weight for each vertex, I am counting on 4 per bone, but maybe I should not have that last loop at all?
Im not sure how to set it up.
I would appreciate some help, thank you very much.
for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
for (UINT n = 0; n < 4; n++)
{
//if
}
}
}
//////////////////////////////////////////////
for (UINT k = 0; k < currentMesh->mNumVertices; k++)
{
Vertex vert;
vert.position.x = currentMesh->mVertices[k].x;
vert.position.y = currentMesh->mVertices[k].y;
vert.position.z = currentMesh->mVertices[k].z;
vert.TexCoord.x = currentMesh->mTextureCoords[0][k].x;
vert.TexCoord.y = currentMesh->mTextureCoords[0][k].y;
vert.normal.x = currentMesh->mNormals[k].x;
vert.normal.y = currentMesh->mNormals[k].y;
vert.normal.z = currentMesh->mNormals[k].z;
vertexVector.push_back(vert);
}
for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
if (vertexVector[weight.mVertexId].joints.x == 0)
{
vertexVector[weight.mVertexId].joints.x = k;
vertexVector[weight.mVertexId].weights.x = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.y == 0)
{
vertexVector[weight.mVertexId].joints.y = k;
vertexVector[weight.mVertexId].weights.y = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.z == 0)
{
vertexVector[weight.mVertexId].joints.z = k;
vertexVector[weight.mVertexId].weights.z = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.w == 0)
{
vertexVector[weight.mVertexId].joints.w = k;
vertexVector[weight.mVertexId].weights.w = weight.mWeight;
}
}
}
I accessed the same structure I already have and added the stuff afterwards.

What is the time complexity of this function?

Here's a sample solution for Sliding Window Maximum problem in Java.
Given an array nums, there is a sliding window of size k which is
moving from the very left of the array to the very right. You can only
see the k numbers in the window. Each time the sliding window moves
right by one position.
I want to get the time and space complexity of this function. Here's what I think would be the answer:
Time: O((n-k)(k * logk)) == O(nklogk)
Space (auxiliary): O(n) for return int[] and O(k) for pq. Total of O(n).
Is this correct?
private static int[] maxSlidingWindow(int[] a, int k) {
if(a == null || a.length == 0) return new int[] {};
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
// max heap
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
int[] result = new int[a.length - k + 1];
int count = 0;
// time: n - k times
for (int i = 0; i < a.length - k + 1; i++) {
for (int j = i; j < i + k; j++) {
// time k*logk (the part I'm not sure about)
pq.offer(a[j]);
}
// logk
result[count] = pq.poll();
count = count + 1;
pq.clear();
}
return result;
}
You're right in most of the part except -
for (int j = i; j < i + k; j++) {
// time k*logk (the part I'm not sure about)
pq.offer(a[j]);
}
Here total number of executions is log1 + log2 + log3 + log4 + ... + logk. The summation of this series -
log1 + log2 + log3 + log4 + ... + logk = log(k!)
And second thought is, you can do it better than your linearithmic time solution using double-ended queue property which will be O(n). Here is my solution -
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || k <= 0) {
return new int[0];
}
int n = nums.length;
int[] result = new int[n - k + 1];
int indx = 0;
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
// remove numbers out of range k
while (!q.isEmpty() && q.peek() < i - k + 1) {
q.poll();
}
// remove smaller numbers in k range as they are useless
while (!q.isEmpty() && nums[q.peekLast()] < nums[i]) {
q.pollLast();
}
q.offer(i);
if (i >= k - 1) {
result[indx++] = nums[q.peek()];
}
}
return result;
}
HTH.

2 dimensional List<T> in C++\CLI

I want to create a 2-dimensional List in C++\CLI. Question is how to declare it?
I have tried this:
List<List<int>^>^ H = gcnew List<List<int>>(); // Scoring matrix H
H->Add(gcnew List<int>() );
for (i = 0; i < n; i++) // Fill matrix H with 0
{
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Then I get a lot of syntax errors, starting with this one:
error C3225: generic type argument for 'T' cannot be 'System::Collections::Generic::List', it must be a value type or a handle to a reference type
In this declaration
List<List<int>^>^ H = gcnew List<List<int>>();
The right type specifier does not correspond to the left type specifier. Should be
List<List<int>^>^ H = gcnew List<List<int>^>();
With advice from Hans and Vlad, this seems to work:
List<List<int>^>^ H = gcnew List<List<int>^>(); // Scoring matrix H
for (i = 0; i < n; i++) // Fill matrix H with 0
{
H->Add(gcnew List<int>() );
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Thx, Jan

Codility extreme large Number error

I have a Codility test to take soon.
I was trying to find a modification in the code to avoid EXTREME LARGE NUMBERS ERROR by using LONG instead of INT... but this did not work.
Has anybody tried using CODILITY demo test and get a 100?
I went through previous posts but no solution to this particular problem.
MY CODE: COMPLEXITY O(N)... Still I got 94.
// you can also use includes for example:
// #include <algorithm>
#include<iostream>
#include<vector>
#include<math.h>
int equi ( const vector<int> &A ) {
if((int)A.size()==0)
return -1;
long int sum_l = A[0];
long int total_sum =0;
for(int i =0; i<(int)A.size();i++){
total_sum = total_sum + A[i];
}
int flag =0;
total_sum = total_sum -A[0];
if(total_sum == 0)
return 0;
for(int i=1; i<(int)A.size()-1;i++){
total_sum = total_sum - A[i];
if(sum_l ==total_sum){
flag=1;
return i;
}
sum_l= sum_l + A[i];
}
if(sum_l ==0)
return (int)A.size()-1;
if(flag ==0)
return -1;
}
I used long long, and I had not problem.
Try this one.
int left = A[0];
int right = 0;
for(int i: A){
right += i;
}
right -= left;
int diff = Math.abs(left - right);
for (int i = 1; i < A.length-1; i++) {
left += A[i];
right -= A[i];
int a = Math.abs(left - right);
if(diff > a){
diff = a;
}
}
return diff;